CommonJS--Require

Source: Internet
Author: User

requireThe command is used to load the file, and the suffix name defaults to .js .

var foo = require (' foo ');//  equivalent to var foo = require (' foo.js ');

Depending on the format of the parameters, the require command goes to different paths to find the module files.

(1) If the argument string starts with "/", it means that a module file is loaded in the absolute path. For example, it require(‘/home/marco/foo.js‘) will load /home/marco/foo.js .

(2) If the argument string begins with "./", it means that a module file is loaded in a relative path (as opposed to the location of the current execution script). For example, the require(‘./circle‘) current script will be loaded in the same directory circle.js .

(3) If the argument string does not start with "./" or "/", it means that a default supplied core module (located in the system installation directory of node) is loaded, or an installed module (global or partial) that is located in the Node_modules directory at all levels.

For example, the script /home/user/projects/foo.js executes the require(‘bar.js‘) command, and node searches for the following files in turn.

    • /usr/local/lib/node/bar.js
    • /home/user/projects/node_modules/bar.js
    • /home/user/node_modules/bar.js
    • /home/node_modules/bar.js
    • /node_modules/bar.js

The purpose of this design is to enable different modules to localize the modules they depend on.

(4) If the argument string does not start with "./" or "/", and is a path, for example require(‘example-module/path/to/file‘) , the location will be found first example-module , and then it is the parameter to find the subsequent path.

(5) If the specified module file is not found, node tries to add,, and .js .json then search for the file name .node . Files are parsed in a text-formatted JavaScript script file, and the file is parsed in a .js .json JSON-formatted text file, and the .node file is parsed with a compiled binary file.

(6) If you want to get require the exact file name of the command load, use the require.resolve() method.

CommonJS--Require

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.