What is the difference between require() vs module.require() in nodejs ?
The documentation don't provide any content about the difference between them. Can anyone explain ?
What is the difference between require() vs module.require() in nodejs ?
The documentation don't provide any content about the difference between them. Can anyone explain ?
They both require other modules (call other module's and returns their module.exports objects)
module.require('./module1.js');
require('./module1.js');
but they are NOT the same
require === module.require // -> false
module.require can only be used as method
require is a method but also has their own properties (cache main)
providing additional data
module.require.cache // -> undefined
require.cache // -> {the cached module}
The official documentation is not only a little unclear on how they differ, but also lacks any example usage that would help determine the usefulness of using module.require vs require. So I resorted to experimentation, and what I discovered is that, essentially, require() takes a path relative to the module doing the importing, where as module.require takes a module id (this is outlined in the docs, but it is a little unclear what 'id' refers to).
So what id actually refers to is an attribute 'id' of the module object (module.id). If you console.log(module.id) from within a module, you will get the full path to that file. But the only way to load a module using module.require() is if you have access to the module.id variable, which is not exported in module.exports. So technically you could load the current module inside itself as so:
Example Usage 1:
test.js
module.exports = {};
module.exports.foo = function() {
console.log('bar');
};
let thisModule = module.require(module.id);
thisModule.foo();
app.js
const test = require('./test');
output
$ bar
How this use case is useful is beyond me, but loading a module inside itself to run its own exported functions is possible for some reason. Another possible use case is something like this:
Example Usage 2:
test.js
const loader = require('./loader'); // Some library that handles module loading
// dynamically. loader.register passes the module
// info to the dynamic loader to be called
// later by the client perhaps.
loader.register(module);
module.exports = {};
module.exports.foo = function() {
console.log('bar');
};
app.js
const loader = require('./loader');
const test = loader.require('./test'); // loader module will have to do some parsing
// to match './test' with the fully qualified
// path name. Just to be clear, loader.require
// is some function defined by the library
test.foo();
Theoretically outputs bar. So I guess the second use case could be useful if you want to control the loading of the modules for some reason. But you would have to build your modules specifically to this usage. I imagine there could be other uses, I plan to investigate further and if I discover anything new, then I will expand this answer. I hope this helps.
https://nodejs.org/api/modules.html#modules_module_require_id
The module.require() method provides a way to load a module as if require() was called from the original module.
In order to do this, it is necessary to get a reference to the module object. Since require() returns the module.exports, and the module is typically only available within a specific module's code, it must be explicitly exported in order to be used.
What this is saying is that you can require() as if you are in another module, given that you somehow have access to the other module's module variable.
For example, I can do a module.require('./c'), even if c.js doesn't exist as a sibling in my current directory. It will work as long as module's directory has c.js in it.
Example:
.
├── a
│ ├── c.js
│ └── index.js
└── b.js
// b.js
console.log(require('./a').require('./c'));
// a/index.js
module.exports = module;
// a/c.js
module.exports = 'c';
node b.js // stdout 'c'
Documentation for module.require is clear:
The module.require method provides a way to load a module as if require() was called from the original module. In order to do this, it is necessary to get a reference to the module object. Since require() returns the module.exports, and the module is typically only available within a specific module's code, it must be explicitly exported in order to be used.