I tried this:
// mod.js
var a = 1;
this.b = 2;
exports.c = 3;
// test.js
var mod = require('./mod.js');
console.log(mod.a); // undefined
console.log(mod.b); // 2
console.log(mod.c); // 3, so this === exports?
So I image that require() may be implement like this:
var require = function (file) {
var exports = {};
var run = function (file) {
// include "file" here and run
};
run.apply(exports, [file]);
return exports;
}
Is that right? Please help me to understand require(), or where can I find the source code. Thanks!
Andrey showed the source code, but if you also wonder how to use it, the easy and simple explanation is here (http://nodejs.org/api/modules.html).
These were two good examples for me.
My favourite pattern is
The require is a function that takes one argument called path, in this case the path is
./mod.js
when the require is invoked, a sequences of tasks are happened:
call
Module.prototype.require
function declared in lib/module.js which assert that the path exists and was a stringcall
Module._load
which is a function in lib/module.js that resolve the file throughModule._resolveFilename(request, parent, isMain)
,Module._resolveFilename
function is called and checks if the module is native (The native modules are returned byNativeModule
function defined in lib/internal/bootstrap_node.js), if yes it will return the module else it checks the number of characters of the parh (Must 2 character at least) and some characters (the path must started by./
) viaModule._resolveLookupPaths
function defined in defined in lib/internal/bootstrap_node.jsvar module = new Module(filename, parent);
NativeModule.prototype.compile
defined in lib/internal/bootstrap_node.jsNativeModule.wrap
defined in lib/internal/bootstrap_node.js takes the javascript content compiled ofmod.js
and wraps it : It wraps it in some other code that makes all this work. So the code you've written inmod.js
is wrapped in a function expression. that means everything you write in node is run in V8The source is available here next to the downloads : http://nodejs.org/ exports/require are keywords, I don't think they are coded in javascript directly. Node is coded in C++ , javascript is just a scripting shell around the C++ core.
Source code is here.
exports
/require
are not keywords, but global variables. Your main script is wrapped before start in a function which has all the globals likerequire
,process
etc in its context.Note that while module.js itself is using
require()
, that's a different require function, and it is defined in the file called "node.js"Side effect of above: it's perfectly fine to have "return" statement in the middle of your module (not belonging to any function), effectively "commenting out" rest of the code