The JavaScript specification (ECMAScript) does not define a complete standard library that can be used by most programs. CommonJS provides a set of JavaScript standard library specifications. Node implements the CommonJS specification.
Module Basics
In Node, modules and files have a one-to-one correspondence. We define a module:
Module loading
Node will buffer loaded modules to avoid the overhead of loading again:
Load module test.js multiple times
When loading a file without a suffix, Node will try to add a suffix and load:
1..js (JavaScript source file)
2..node (C/C extension module)
3..json (JSON file)
There are several main categories of modules:
1. Core module. The core modules have been compiled into Node, and we can find these core modules in the lib directory in its source code. Common core modules: net, http, fs modules, etc.
2. File module. File modules are loaded via a relative or absolute path, such as circle.js
we saw above3. Custom module. Custom modules are located in the node_modules directory, and the various modules we install through npm are placed in the node_modules directory
The core module is always loaded first. If there is a custom module http, then the core module http will still be loaded instead of the custom module http. When loading a custom module, first look for the node_modules directory in the current directory, then the node_modules directory under the parent directory, and so on until the root directory.
require When the loaded module is not a file, but a directory, such a directory is called a package. There is a file named package.json (package description file) in the package, for example:
Where main indicates the module that needs to be loaded. If package.json does not exist in the package or the main module is not specified in package.json, Node will try to load index.js, index.node, and index.json.
When loading a JavaScript module, the loaded module is wrapped in a function:
The module, exports, __filename, __dirname, etc. accessed by each JavaScript module are actually passed in through function parameters. Because of this wrapping, the local variables of the module are inaccessible from outside. But sometimes there are problems that are difficult to understand, such as:
test1.js
test2.js
Load these two modules:
Exports is passed to the module as a parameter. We can naturally add attributes (or methods) to the exports object through exports.x, but directly assigning a value to exports (for example, exports = x) only changes the formal parameters rather than the actual parameters. value, therefore:
1. When adding attributes to exports, use exports
2. When assigning values to exports, use module.exports
Pack
According to CommonJS specifications, a complete package should contain:
1.package.json package description file
2.bin binary file directory
3.lib JavaScript code directory
4.doc document directory
5.test test code directory
NPM is a package management tool for Node. Common usage:
View the documentation for the command:
See the documentation for the install command.
Install a package:
Remove a package: