How does require () work in node.js?

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, an image requiring () can be implemented as follows:

 var require = function (file) { var exports = {}; var run = function (file) { // include "file" here and run }; run.apply(exports, [file]); return exports; } 

It is right? Please help me understand require () or where I can find the source code. Thank!

+70
javascript this require apply
Feb 28 2018-12-12T00:
source share
4 answers

The source code is here . exports / require are not keywords, but global variables. Your main script is wrapped before start in a function that has all global variables, such as require , process , etc. in its context.

Note that while module.js itself uses require() , another function requires it, and it is defined in a file called "node.js"

The side effect is higher: great to have a “return” in the middle of your module (not belonging to any function), effectively “commenting out” the rest of the code

+51
Feb 28 '12 at 7:08
source share

Andrey showed the source code, but if you are also wondering how to use it, a simple and simple explanation is here ( http://nodejs.org/api/modules.html ).

These were two good examples for me.

 //foo.js, multiple methods var circle = require('./circle.js'); console.log( 'The area of a circle of radius 4 is ' + circle.area(4)); //circle.js var PI = Math.PI; exports.area = function (r) { return PI * r * r; }; exports.circumference = function (r) { return 2 * PI * r; }; //bar.js var square = require('./square.js'); var mySquare = square(2); console.log('The area of my square is ' + mySquare.area()); //square.js, single method module.exports = function(width) { return { area: function() { return width * width; } }; } 

My favorite pattern

 (function (controller) { controller.init = function (app) { app.get("/", function (req, res) { res.render("index", {}); }); }; })(module.exports); 
+8
Dec 24 '14 at 8:33
source share
 var mod = require('./mod.js'); 

A requirement is a function that takes a single argument called path, in this case the path ./mod.js

when require is called, the following task sequences are executed:

  • call Module.prototype.require function declared in lib / module.js that claim that the path exists and was a string

  • calling Module._load , which is a function in lib / module.js that resolves the file through Module._resolveFilename(request, parent, isMain) ,

  • the Module._resolveFilename function is Module._resolveFilename and it is checked whether the module is native (native modules are returned by the NativeModule function defined in lib / internal / bootstrap_node.js ), if so, it will return the module yet, it checks the number of parh characters (at least 2 characters ) and some characters (the path should start with ./ ) through the Module._resolveLookupPaths function defined in lib / internal / bootstrap_node.js
  • check the directory containing the file
  • If the path contains an extension (in our example, yes: mod.js), the basename function defined in lib / path.js checks if the extension is " js "
  • then it will create a new module for the file specified in the argument var module = new Module(filename, parent);
  • content will be compiled through v8 via the NativeModule.prototype.compile function defined in lib / internal / bootstrap_node.js
  • NativeModule.wrap defined in lib / internal / bootstrap_node.js takes javascript content made up of mod.js and wraps it: it wraps it in some other code that makes it all work. Thus, the code you wrote in mod.js is wrapped in a function expression. this means that everything you write in node runs in V8
  • Returns the .exports module.
+6
Oct 24 '17 at 19:09
source share

The source is available here next to the downloads: http://nodejs.org/ export / require - these are keywords, I don’t think they are encoded directly in javascript. Node is encoded in C ++, javascript is just a shell of scripts around the C ++ core.

-9
Feb 28 '12 at 4:00
source share



All Articles