Home > Web Front-end > JS Tutorial > Nodejs minimalist introductory tutorial (1): module mechanism_node.js

Nodejs minimalist introductory tutorial (1): module mechanism_node.js

WBOY
Release: 2016-05-16 16:32:52
Original
1107 people have browsed it

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:

Copy code The code is as follows:

// circle.js
var PI = Math.PI;

//Export function area
exports.area = function(r) {
Return PI * r * r;
}

//Export function circumference
exports.circumference = function(r) {
Return 2 * PI * r;
};

Here, add the functions that need to be exported to the exports object. Local variables of a module are not accessible from outside (for example, the PI variable in the example above). Call require to load the module circle.js:
Copy code The code is as follows:

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

By the way, in the module, there is a module object, which represents the module itself, and exports is the attribute of the module.

Module loading

Node will buffer loaded modules to avoid the overhead of loading again:

Copy code The code is as follows:

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

Load module test.js multiple times

Copy code The code is as follows:

// Only output "I'm here"
once require('./test');
require('./test');

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 above

3. 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:

Copy code The code 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 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:

Copy code The code 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 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

Copy code The code is as follows:

exports = {
name: 'Name5566',
}

test2.js

Copy code The code is as follows:

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

Load these two modules:

Copy code The code is as follows:

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 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:

Copy code The code is as follows:

npm help install

See the documentation for the install command.

Install a package:

Copy code The code is as follows:

npm install redis

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

Remove a package:

Copy code The code is as follows:

npm remove redis

Remove the redis package. The remove command will remove the package in the current directory.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template