1. Environment
First, let's take a look at what nodejs mainly needs to execute
- CommonJS
commonjs is the module specification used by nodejs
- global object
We know that JavaScript running in the browser contains BOM and DOM, of which window is a global object, but nodejs does not have BOM and DOM, and there is no such global object as window. Instead, it is global, which also has some attributes and method for us to use.
- process
Represents the currently executing process, hanging under global
1.1 CommonJS
- Each file is a module and has its own scope
- Inside a module the module variable represents the module itself
- The module.exports attribute represents the external interface of the module
To understand the above, let's write a piece of code like this
Then run it on the command line and enter the following line of code to enter debug mode (--inspect enters debug mode, -brk stops at the beginning)
Then we open chrome to enterchrome://inspect
, press Enter, you can see the following interface
Click inspect to enter the debugging interface
As we can see from the figure, when the code we actually execute is running in nodejs, it is wrapped by a parenthesis and a semicolon, and inside is a function, which is done by nodejs for us (the scope of the module itself). ), this function passes in 5 variables, which are
- exports
Note that you can't point the exports variable directly to a value, because doing so would cut exports from module.exports.
The following is invalid, because exports no longer points to module.exports.
exports = function(x) {console.log(x)};
复制代码
The following writing hello function cannot be exported, because module.exports has been reassigned.
exports.hello = function() {
return 'hello';
};
module.exports = 'Hello world';`
复制代码
This means that if the external interface of a module is a single value, it cannot be exported using exports, but can only be exported using module.exports.
- require
- module
- __filename
- __dirname
2. require
2.1 require rules
- / means absolute path, ./ means relative to the current file
- Support js, json, node extensions, try not to write in turn
- If the path is not written, it is considered to be a build-in module or a third-party module in node_modules at all levels
2.2 require feature
- Executed when the module is loaded, cached after loading
For example, let's write a module first
This module exports a variable num and prints a 'hello' at the beginningNext, we import this module in another file
run this file As you can see, we loaded the demo module twice, and hello was only printed onceSo, the module will only be executed when it is loaded for the first time
- Once a module is loaded cyclically, only the executed part will be output, and the unexecuted part will not be output
This definition is a bit difficult to understand, but that's okay, let's understand it by example
Let's write three js files first
Then we execute the main.js file, the steps are as follows
-
First introduced the a.js file, let's load the a.js file
-
In the first line of a.js, x is assigned 'a1', and then b.js is introduced, and we go to load the b.js file
-
In the first line of b.js, assign x to 'b1', and then import the a.js file, which constitutes a circular reference, so only part of the content of a.js can be referenced, that is
exports.x='a1'
, assign x to ' a1', so first print outb.js a1
-
Then, a.js is printed out
a.js b2
, because b.js has been executed, so the referenced x is b2 -
In the main.js file, since both a.js and b.js have been executed, print out
main.js a2
,main.js b2
Control point final print
Reprinted at: https://juejin.im/post/5d00a0336fb9a07ec27b9c3a