Modules are the basic components of Node.js applications. Files and modules are in one-to-one correspondence. A Nodejs module is a file, and this file may be JavaScript code, JSON or compiled "C/ C" extension, the reference module can be used with the "require('file path')" statement.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.
In order to allow Node.js files to call each other, Node.js provides a simple module system.
Modules are the basic components of Node.js applications, and files and modules have a one-to-one correspondence. In other words, a Node.js file is a module, which may be JavaScript code, JSON, or a compiled C/C extension.
For nodejs, a file is a module. You can export the interface or require other modules to come in.
// module1.js exports.func1 = function(){ console.log('func1 from module1 called'); }
Module1 uses function func1 as the public access interface of the module through the exports object.
//module2.js var in_module1 = require('./module1.js'); in_module1.func1(); exports.func2 = function(){ console.log('func2 from module2 called'); }
module2 requires module1 in. At this time, in_module1 is equivalent to the exports object of module1. When using in_module1 to call func1, it is equivalent to calling func1 through the exports object of module1.
At the same time, module2’s own function func2 also serves as the module2 public interface through the module’s exports object.
// module3.js var in_module2 = require('./module2.js'); in_module2.func2();
Similarly, module3 requires module2. At this time, in_module2 is equivalent to the exports object of module2.
The running results are as follows:
rlan@rlan-LA:~/nodejs/nodetest$ node module2.js func1 from module1 called rlan@rlan-LA:~/nodejs/nodetest$ node module3.js func1 from module1 called func2 from module2 called
nodejs importing a module not only gets the public interface of the module, but also references other statements in the file, such as:
module1 .js is changed to
// module2.js console.log('this is in module2'); var in_module1 = require('./module1.js'); in_module1.func1(); exports.func2 = function(){ console.log('func2 from module2 called'); }
module2 introduces the func1 function of module1 and executes the print statement in module1:
rlan@rlan-LA:~/nodejs/nodetest$ node module1.js this is in module1 rlan@rlan-LA:~/nodejs/nodetest$ node module2.js this is in module2 - module2 self this is in module1 - require module1 func1 from module1 called - module2 self
Now, module2 loads module1 and module3 loads module2, what will happen if module3 loads module1 again?
// module3.js var in_module1 = require('./module1.js'); var in_module2 = require('./module2.js'); in_module1.func1(); in_module2.func2();
At this time, module3 first loads module1, then module2, and module2 itself loads the part of module1. The running result is
rlan@rlan-LA:~/nodejs/nodetest$ node module3.js this is in module1 - require module1 this is in module2 - require module2 func1 from module1 called - require module2 func1 from module1 called - module3 self func2 from module2 called - module3 self
If the require order of module3 is adjusted Look:
// module3.js var in_module2 = require('./module2.js'); var in_module1 = require('./module1.js'); in_module1.func1(); in_module2.func2();
The running result is:
rlan@rlan-LA:~/nodejs/nodetest$ node module3.js this is in module2 - require module2 this is in module1 - require module2 func1 from module1 called - require module2 func1 from module1 called - module3 self func2 from module2 called - module3 self
It seems that nodejs uses some mechanism to ensure that the same module will not be loaded repeatedly in another module, so
this is in module1
This line only appears once, although it seems to be loaded twice in module3.js.
So, what happens if there is a loop loading? Now we let module1 require module2:
// module1.js console.log('this is in module1'); var in_module2 = require('./module2.js'); exports.func1 = function(){ console.log('func1 from module1 called'); }
// module2.js console.log('this is in module2'); var in_module1 = require('./module1.js'); in_module1.func1(); exports.func2 = function(){ console.log('func2 from module2 called'); }
The running results are as follows:
rlan@rlan-LA:~/nodejs/nodetest$ node module1.js this is in module1 this is in module2 /home/rlan/nodejs/nodetest/module2.js:4 in_module1.func1(); ^ TypeError: in_module1.func1 is not a function at Object.<anonymous> (/home/rlan/nodejs/nodetest/module2.js:4:12) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/rlan/nodejs/nodetest/module1.js:3:18) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) rlan@rlan-LA:~/nodejs/nodetest$ node module2.js this is in module2 this is in module1 func1 from module1 called
nodejs seems to prevent the behavior of loading itself. When running module2, the behavior is the same as the result of module1 not loading module2. Same, no error reported. When running module1, when I went to module2 and ignored the require module1 statement, module2 called func1 of module1, and the program went wrong.
To sum up, nested require statements that repeatedly load modules (or load themselves) in nodejs cannot be executed correctly.
[Recommended learning: "nodejs tutorial"]
The above is the detailed content of what is nodejs module. For more information, please follow other related articles on the PHP Chinese website!