Grab all require calls from a file - node.js

I'm writing a small Node.js module that does some stuff on a specific file.
I'm wondering if it's possible to grab the names of all modules that have been require'd - I looked through Browserify's codebase but I couldn't really get a grasp on what was happening.
As an example, say I have this file:
var fs = require('fs'),
chalk = require('chalk');
// Some code
var temp_dir = require('os-tmpdir')();
// Maybe some more code
Is it possible to read in this file (or filename) and get an output like:
['fs', 'chalk', 'os-tmpdir']

Sorry, just noticed the node module required.

Related

Is there a reason for using var instead of const in Gulp config files?

According to the answers to this question Const in JavaScript: when to use it and is it necessary? it would make sense to use const for gulpfile.js configuration, would it not?
But in every usage description of gulp-modules, I see var being used like for instance:
var gulp = require('gulp');
var argv = require('yargs').argv;
var autoprefixer = require('gulp-autoprefixer');
//...
As I understand it, these declarations never change while Gulp is running.
Is there a reason for using var? Is it something with node.js compiler? Or is this just a habit?
It's a necessary habit.
gulp is a module and can be used in multiple locations in a code file. But, if you overwrite it accidentally the whole code will crash.
It's better to depend on the computer or compiler. Instead of human accuracy.
If you use const , you can never overwrite the variable. That will avoid error.
I suppose you are not using babel with gulp. If you are using it with babel, it would be better to use const or let instead of var.
If you are not using babel, then var is the only way for you. Read this blog post for further information: https://travismaynard.com/writing/using-babel-with-gulp
Or even better, this https://markgoodyear.com/2015/06/using-es6-with-gulp/

how can VS code support Go to definition

I have two JavaScript files: config.js, app.js. In app.js I want to use function defined in config.js so I could use require().
config.js
module.exports = {
somefunc: somefunc
}
app.js
var config = require('./config')
But I don't want to input the './' every time so I add a myRequire.js file.
myRequire.js
global.myRequire = function (p){
return require('./' + p)
}
In that case I could use myRequire('config') next time instead of myRequire('./config'), which might looks more concise.
app.js
require("./myRequire")
var config = myRequire('config')
config.somefunc()
But I met a problem, that I cannot use F12(Go to Definition) in VS Code to find the somefunc function. So could someone tell me what should I do to make it work in this case?
You are introducing a lot more problem than what you are trying to achieve. What if you are requiring a file from different file path '../../here', './over/there'.
If you really want to require something without a path. You can create your own npm module so you can require it globally without paths OR you create a folder with index.js in it and you require all the things you need.

nodejs include required packages in all route files using require() function

Hi I'm new to nodeJs and currently developing a Rest API using node.I'm planning to develop it with a good folder structure, so I can scale it up easily. There I'm using several route files according to the business logic.
ex :- authRoutes,profileRoutes,orderRoutes ......
Currently in every single route file I had to include following codes
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken');
var passport = require('passport');
My problem is , Is it totally fine to use above code segments in all the route files( I'm concerning about the code optimisation/coding standards and execution speed ) or is there any better way to do this.
It's better if you can explain the functionality of require() function.
Thanks
In my experience, this is very standard to do. I suggest reading this question and answer to learn more about the way it affects the speed of your programs.
TL;DR of require()
When your lines of code are ran that include a variable that exists because of a require(), for instance
var https = require('https');
https.get(url, function(response) {...});
The compiler reads it and goes into the https module folder, and looks for the .get function.
However, if you are trying to require() a certain JavaScript file, such as analysis.js, you must navigate to that file from the file you are currently in. For instance, if the file you want is on the same level as the file you are in, you can access it like this:
var analysis = require('./analysis.js');
//Let analysis have a function called analyzeWeather
analysis.analyzeWeather(weather_data);
These lines of code are a little different from above. In this require() statement, we are saying grab the .js file with this name, analysis. Once you require it, you can access any public function inside of that analysis.js file.
Edits
Added require() example for .js file.

Using fs.ReadStream to move a file

I had a question reg. moving a file using Node.
In the end - I have a local file on M drive (Mapped) and want to move/copy it to N drive.
Using fsStream I was able to move it locally, but I'm concerned how it will react once I put it onto a website due to file manipulation.
I'm curious if anyone has any solutions - I originally was using File API, which was beautiful but I noticed all it can do is load the data, I was unable to 'save' it anywhere.
Script is very simple - Mostly I'll be using variables to assign the write-to/read to path. I'm struggling to find info on how to write from server to PC, just a lot of reasons why it can't be done due to violation.
My other option is possibly just using a Powershell script?
var fs = require('fs'),
util = require('util');
var origFile = '~origPath~'
var destFile = '~destPath'
var is = fs.createReadStream(origFile)
var os = fs.createWriteStream(destFile);
util.pump(is, os, function() {
fs.unlinkSync(origFile);
});
Thanks ahead of time. Sorry if my post lacks needed details. I'll edit once I know.

what does var io = require('../..')(server) do?

I've build the project https://github.com/Automattic/socket.io/tree/master/examples/chat locally and it is working great. However, it would be nice to understand a little more about how a socket application works.
In the main startup script one of the modules that is pulled in with require is
var io = require('../..')(server)
what does require('../..') do?
thanks!
When a path to a directory is given to require, it will implicitly look for an index.js in that directory.
In this case, it's the equivalent of
var socket = require("../../index.js");
var io = socket(server);
In the example provided, they're just using some shorthand and throw away the intermediate value returned by the call to require.
Check out the module.require docs for more info.
Here, in your code
require('../..');
Will add File form the path, which have used SOCKET.IO, as you can see that you have not added Socket.io module.
Also, if no specific path give for file or folder, Module require will try to load index.js or index.node. if no such file exist then it will give error.

Resources