The JavaScript specification (ECMAScript) does not define a complete set of standard libraries that can be applied to most programs. CommonJS provides a set of JavaScript standard library specifications. Node implements the CommonJS specification.
Module Basics
In Node, modules and files correspond one by one. We define a module:
The code copy is as follows:
// circle.js
var PI = Math.PI;
// Export function area
exports.area = function(r) {
return PI * r * r;
}
// Export function circuitference
exports.circumference = function(r) {
return 2 * PI * r;
};
Here the functions that need to be exported are added to the exports object. Local variables of the module are not accessible externally (for example, the PI variable in the example above). Call require to load the module circle.js:
The code copy is as follows:
var circle = require('./circle.js');
console.log('The area of a circle of radius 4 is '
+ circle.area(4));
As mentioned, in the module, there is a module object that represents the module itself, and exports is the attribute of the module.
Loading of modules
Node will buffer loaded modules to avoid the overhead of reloading:
The code copy is as follows:
// test.js
console.log("I'm here");
Loading module test.js multiple times
The code copy is as follows:
// Only output "I'm here" once
require('./test');
require('./test');
When the loading file has no suffix, Node tries to add the suffix and load:
1..js (JavaScript source file)
2..node (C/C++ extension module)
3..json (JSON file)
There are several main types 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 module, etc.
2. File module. The file module is loaded through a relative or absolute path, such as the circle.js we saw above
3. Customize the module. The custom module is located in the node_modules directory. The various modules we installed through npm are placed in the node_modules directory.
Core modules are 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. When loading a custom module, first look for the node_modules directory in the current directory, then look for the node_modules directory in the parent directory, and so on until the root directory.
require When the module loaded 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:
The code copy is as follows:
{ "name" : "some-library",
"main" : "./lib/some-library.js" }
where main indicates the module that needs to be loaded. If package.json does not exist in the package or package.json does not specify main module, then Node will try to load index.js, index.node, index.json.
When loading a JavaScript module, the loaded module is wrapped in a function:
The code copy is as follows:
function(module, exports, __filename, __dirname, ...) {
JavaScript module
}
The module, exports, __filename, __dirname, etc. accessed by each JavaScript module are actually passed in through function parameters. Because of this kind of package, local variables of the module are inaccessible externally. But sometimes there are problems that are difficult to understand, such as:
test1.js
The code copy is as follows:
exports = {
name: 'Name5566',
}
test2.js
The code copy is as follows:
module.exports = {
name: 'Name5566',
}
Load these two modules:
The code copy is as follows:
var test1 = require('./test1.js');
console.log(test1.name); // undefined
var test2 = require('./test2.js');
console.log(test2.name); // Name5566
exports are passed as parameters to the module. 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) just changes the value of the formal parameter rather than the actual parameter, so:
1. When adding properties to exports, use exports
2. When assigning a value to exports, use module.exports
Bag
According to CommonJS specification, a complete package should include:
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 command documentation:
The code copy is as follows:
npm help install
View the documentation for the command install.
Install a package:
The code copy is as follows:
npm install redis
Install the redis package. The install command installs the package in the node_modules directory in the current directory.
Remove a package:
The code copy is as follows:
npm remove redis
Remove the redis package. The remove command will remove the package in the current directory.