Nodejs minimal introduction tutorial (I) : module mechanism

  • 2020-03-30 04:10:16
  • OfStack

The JavaScript specification (ECMAScript) does not define a comprehensive set of standard libraries that will work for most programs. CommonJS provides a set of JavaScript standard library specifications. Node implements the CommonJS specification.

Module based

In Node, modules and files are one-to-one. We define a module:


// circle.js
var PI = Math.PI;
 
//The output function area
exports.area = function(r) {
    return PI * r * r;
}
 
//The output function circumference
exports.circumference = function(r) {
    return 2 * PI * r;
};

The exported function is added to the exports object. The module's local variables cannot be accessed from outside (for example, the PI variable in the previous example). Require to load the module circle.js:

var circle = require('./circle.js');
console.log('The area of a circle of radius 4 is '
           + circle.area(4));

Incidentally, in a module, there is a module object that represents the module itself, and exports is the property of the module.

Module loading

Node buffers loaded modules to avoid the overhead of reloading:


// test.js
console.log("I'm here");

Load the module test.js multiple times


//Output "I'm here"
only once require('./test');
require('./test');

When the load 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 mainly several types of modules:

1. Core module. The core modules have been compiled into Node and can be found in the lib directory of its source code. Common core modules: net, HTTP, fs module, etc

2. File module. The file module is loaded via a relative or absolute path, such as the circle.js we saw above

3. Custom module. The custom modules are located in the node_modules directory, where the various modules we installed through NPM are located

The core module is always loaded first, and if there is a custom module HTTP, the core module HTTP will still be loaded instead of the custom module HTTP. When a custom module is loaded, first look for the node_modules directory in the current directory, then the node_modules directory in the parent directory, and so on, all the way to the root directory.

When the module that requires to be loaded is not a file but a directory, such a directory is called a package. There is a file called package.json in the package, for example:


{ "name" : "some-library",
  "main" : "./lib/some-library.js" }

Where main indicates the modules that need to be loaded. If package.json does not exist in the package or if the main module is not specified in package.json, Node will attempt to load index.js, index.node, and index.json.

When a JavaScript module is loaded, the loaded module is wrapped in a function:


function(module, exports, __filename, __dirname, ...) {
    JavaScript module
}

Each JavaScript module to access the module, exports, __filename, __dirname is actually passed through the function parameters. Because of this package, it is not accessible outside of the module's local variables. But sometimes there are difficult questions, such as:

Test1. Js


exports = {
    name: 'Name5566',
}

Test2. Js


module.exports = {
    name: 'Name5566',
}

Load the two modules:


var test1 = require('./test1.js');
console.log(test1.name); // undefined
var test2 = require('./test2.js');
console.log(test2.name); // Name5566

Exports is passed to the module as a parameter. We can naturally add attributes (or methods) to a exported object through exports.

1. When you add an attribute to exports, you use exports
2. When you assign a value to exports, you use module.exports

package

According to the CommonJS specification, a complete package should include:

1. Package. Json package description file
2. Bin binary file directory
3. Lib JavaScript code directory
4. Directory of doc documents
5. Test code directory

NPM is a package management tool for Node. Common usage:

View the documentation of the command:


npm help install

View the documentation for the command install.

Install a package:


npm install redis

Install the redis package. The install command installs the package in the node_modules directory under the current directory.

Remove a package:


npm remove redis

Remove the redis package. The remove command removes packages from the current directory.


Related articles: