Both these require
statements appear to work the same way:
var Mypackage = require('mypackage.js');
var Mypackage require('mypackage');
Is there a difference between them?
Here is the answer:
Module.prototype.load = function(filename) {
debug('load ' + JSON.stringify(filename) +
' for module ' + JSON.stringify(this.id));
assert(!this.loaded);
this.filename = filename;
this.paths = Module._nodeModulePaths(path.dirname(filename));
var extension = path.extname(filename) || '.js';
if (!Module._extensions[extension]) extension = '.js';
Module._extensions[extension](this, filename);
this.loaded = true;
};
http
, fs
, etc.)
Always takes the precedence in the loading modules.http
, fs
, etc.), Node.js will then begin to search for a directory named, node_modules
. node_modules
folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for examplenode_modules
and
utils
in the following ways:
./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
NODE_PATH
set on your machine(obviously set by Node.JS installer file if you are on windows)
Not Found in all the above steps then, prints a stack trace to stderError:
Cannot find module 'yourfile'