Node. JS Module System

Source: Internet
Author: User

Writing a slightly larger program typically blocks the code. node. JS provides a simple modular system. A module can be either a file or a directory that contains one or more files.

creation of modules

    • If the module is a file, the code is generally reasonably split into different JS files, each file is a module, and the file path is the module name.
    • If the module is a directory, node will usually find a file called Index.js in this directory as the portal for the module (this default setting can be overridden).

 

export of modules 

The node module allows you to select the functions and variables to be exposed to the program from the file being introduced. If the module returns more than one function or variable, it can be specified by setting the properties of the exports object. However, if the module returns only one function or variable, you can set the Module.exports property.

A case in which a function or variable is referenced is multiple:

Reference to a function object, but here is an example of the error:

  

exports = Currency should be changed to module.exports = Currency;

This is because Module.exports is eventually exported in the program. Exports is just a global reference to Module.exports, initially defined as an empty object that can add properties. So Exports.myfunc is just a shorthand for module.exports.myFunc. So, if exports is set to something else, it breaks the citation relationship between Module.exports and exports.

Module Path parsing rules

The Require function supports the absolute path at the beginning of the slash (/) or drive letter (C:), and also supports the./Start relative path. However, these two paths establish a strong coupling between the modules, once the storage location of a module file needs to be changed, the code of the other modules using the module also needs to be adjusted to become a body. Therefore, the Require function supports the third form of the path, which is similar to Foo/bar, and then parses the path according to the following rules until the module location is found.

1. Built-in Modules

If passed to the Require function is the Nodejs built-in module name, does not do the path resolution, directly returns the internal module's export object, for example require (' FS ').

  

2. Node_modules Directory

Nodejs defines a special Node_modules directory for storing modules. For example, if the absolute path of a module is/home/user/hello.js, when the module is loaded with require (' Foo/bar ') in that module, then Nodejs attempts to use the following path in turn.

/home/user/node_modules/foo/bar
/home/node_modules/foo/bar
/node_modules/foo/bar

3.node_path Environment variables

Similar to the PATH environment variable, NODEJS allows you to specify additional module search paths through the NODE_PATH environment variable. The NODE_PATH environment variable contains one or more directory paths, which are used under Linux: Delimited, used under Windows, and delimited. For example, the following Node_path environment variables are defined:

Node_path=/home/user/lib:/home/lib
When the module is loaded using require (' Foo/bar '), the Nodejs tries the following path in turn.

/home/user/lib/foo/bar
/home/lib/foo/bar

  Module initialization
The JS code in a module is executed only once when the module is first used, and the exported object of the module is initialized during execution. After that, the cached exported object is reused.

  Main module
The module passed to Nodejs by command-line arguments to start the program is called the main module. The main module is responsible for scheduling other modules that compose the entire program to complete the work. For example, when you start a program with the following command, Main.js is the main module.

$ node Main.js

Node. JS Module System

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: info-contact@alibabacloud.com 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.