A file code:
// . 1, a method is the require, its function is used to load the module console.log ( "execution B" ) require('./b.js'); console.log ( "execution C" ) the require ( './c'); // where JS suffix may be omitted // 2, the Node, there is no global scope, only module scope @ 2.1 less than the internal external access // 2.2 less than the external access also the interior var CC = the require ( './ C' ); // this is CC acquired variables at less than C file // console.log (cc.cc); // . 3, the require method has two functions // 3.1, the file is loaded and executed inside the module code // 3.2, get loaded docking module exports file exclusive var EX = the require ( './ B' ); // will execute the file B Add method the console.log (ex.add (30,20 )); // Get the value of the variable B file foo console.log (ex.foo);
B file code
console.log ( "B document executed" ); var foo = "BBB" ; exports.foo=foo; exports.add=function (x,y) { return x*y; }
C file code
console.log ( "C file executed" ); var CC = "I'm a C file"