Nodejs simple getting started tutorial (1): module mechanism _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces the simple getting started course of Nodejs (1): module mechanism. This article describes the basic knowledge of the module, module loading, and Package content, if you need it, you can refer to the JavaScript specification (ECMAScript) without defining 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 is as follows:

// Circle. js
Var PI = Math. PI;

// Export the function area
Exports. area = function (r ){
Return PI * r;

// Export the function circumference
Exports. circumference = function (r ){
Return 2 * PI * r;

Add the function to be exported to the exports object. The local variables of the module cannot be accessed externally (for example, the PI variable in the previous example ). Call require to load the module circle. js:

The Code is as follows:

Var circle = require ('./circle. js ');
Console. log ('the area of a circle of radius 4 is'
+ Circle. area (4 ));

It is mentioned that a module object exists in the module, indicating the module itself, and exports indicates the module attribute.

Module Loading

Node caches loaded modules to avoid overhead of re-loading:

The Code is as follows:

// Test. js
Console. log ("I'm here ");

Multiple Loading modules test. js

The Code is as follows:

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

When the file to be loaded has no suffix, Node will try to add the suffix and load it:

1.. js (JavaScript source file)
2. node (C/C ++ extension module)
3. json (JSON file)

There are several main modules:

1. core modules. The core modules have been compiled into Node. We can find these core modules in the lib directory of the source code. Common core modules: net, http, and fs

2. File module. The file module is loaded through a relative or absolute path, for example, the circle. js shown above

3. Custom module. The custom module is located in the node_modules directory. The 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, the core module http will still be loaded while loading, not the custom module http. When loading a custom module, first find the node_modules directory under the current directory, then find the node_modules directory under the parent directory, and so on until the root directory.

When a module loaded by require is not a file but a directory, such a directory is called a package ). The package contains a file named package. json (package Description file), for example:

The Code is as follows:

{"Name": "some-library ",
"Main": "./lib/some-library.js? 1.1.23 "}

Main indicates the module to be loaded. If the main module is not specified in the package. json or package. json, Node will try to load index. js, index. node, and index. json.

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

The Code is as follows:

Function (module, exports, _ filename, _ dirname ,...){
JavaScript module

The module, exports, _ filename, and _ dirname accessed by each JavaScript module are actually transmitted through function parameters. Because of this package, the module's local variables cannot be accessed externally. However, sometimes there are hard-to-understand problems, such:


The Code is as follows:

Exports = {
Name: 'name5566 ',


The Code is as follows:

Module. exports = {
Name: 'name5566 ',

Load these two modules:

The Code is as follows:

Var test1 = require ('./test1.js ');
Console. log (; // undefined
Var test2 = require ('./test2.js ');
Console. log (; // Name5566

The exports parameter is passed to the module through exports. x can naturally add properties (or methods) to the exports object, but assigning values directly to exports (for example, exports = x) only changes the values of the form parameter rather than the real parameter. Therefore:

1. When adding properties for exports, use exports
2. When assigning values to exports, use module. exports


According to CommonJS specifications, 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 code directory

NPM is a Node package management tool. Common usage:

View the command documentation:

The Code is as follows:

Npm help install

View the install command documentation.

Install a package:

The Code is as follows:

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:

The Code is as follows:

Npm remove redis

Remove the redis package. The remove command removes the packages in the current directory.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.