10. Node. JS Module System

Source: Internet
Author: User
Tags throw exception


################################################################################# #介绍
Node. JS Module System
node. JS provides a simple modular system to allow node. js files to be called each other.
Modules are a basic part of the node. JS application, and the files and modules are one by one corresponding. In other words, a node. js file is a module that may be JavaScript code, JSON, or compiled C/s extensions.

####################################################################################
Create a module
##### #例子:

// 1_xmodule.js function () {    console.log (' foo in 1_xmodule.js running ');}; // 1_main.js var xmodule = require ('./1_xmodule '); Xmodule.foo ();

Execute 1_main.js can call 1_xmodule.js method, example is simple, easy to read
1_xmodule.js through Exports.foo = ... Makes it possible to invoke the Foo method in 1_main.js.

node. JS provides exports and require two objects, where exports is the interface that the module exposes, and require is used to obtain an interface from the outside of a module, the exports object of the acquired module.
# # # #例如:

//Hello.jsfunctionHello () {varname;  This. SetName =function(thyname) {name=Thyname;     };  This. SayHello =function() {Console.log (' Hello ' +name); }; }; Module.exports=Hello;//Main.jsvarHello = require ('./hello ')); Hello=NewHello (); Hello.setname (' Byvoid '); Hello.sayhello (); 


The only change to the module interface is to use Module.exports = Hello instead of Exports.world = function () {}. When the module is referenced externally, its interface object is the Hello object itself to be exported, not the original exports.

####################################################################################
Where are the modules on the server?
Module loading process


# # #从文件模块缓存中加载
Although the native module has a different priority than the file module, it takes precedence to load the existing module from the cache of the file module.
# # #从原生模块加载
The priority level of the native module is second only to the priority of the file module cache. The Require method first checks whether the module is in the list of native modules after parsing the file name. In the case of HTTP modules, although there is a Http/http.js/http.node/http.json file in the directory, require ("http") is not loaded from these files, but is loaded from the native module.
The native module also has a buffer, which is also preferentially loaded from the buffer. If the buffer has not been loaded, then the loading mode of the native module is called to load and execute.
# # #从文件加载
When the file module cache does not exist and is not a native module, node. JS parses the parameters passed in by the Require method and loads the actual file from the file system, and the wrapper and compilation details in the loading process are described in the previous section, where we will describe in detail the process of finding the file module, where There are also some details worth knowing.
The Require method accepts the following types of parameters:
? HTTP, FS, path, and so on, native modules.
?./mod or.. /mod, relative path to the file module.
? /pathtomodule/mod, the absolute path of the file module.
? MoD, non-native module of the file module.
Execute the Require (X) statement execution order under path Y:
1. If X is a built-in module
A. Returning a built-in module
B. Cessation of implementation
2. If X starts with '/'
A. Setting Y to the file root path
3. If X takes './' or '/' or '. /' Start
A. Load_as_file (Y + X)
B. load_as_directory (Y + X)
4. Load_node_modules (X, DirName (Y))
5. Throw exception "not found"
Load_as_file (X)
1. If x is a file, load X as JavaScript text and stop execution.
2. If X.js is a file, load X.js as JavaScript text and stop execution.
3. If X.json is a file, parse X.json as a JavaScript object and stop execution.
4. If X.node is a file, load the X.node as a binary plug-in and stop execution.
Load_index (X)
1. If X/index.js is a file, load X/index.js as JavaScript text and stop execution.
2. If X/index.json is a file, parse X/index.json as a JavaScript object and stop execution.
3. If X/index.node is a file, load the X/index.node as a binary plug-in and stop execution.
Load_as_directory (X)
1. If X/package.json is a file,
A. Parse the X/package.json and look for the "main" field.
B. Let M = X + (JSON main field)
C. Load_as_file (M)
D. Load_index (M)
2. Load_index (X)
Load_node_modules (X, START)
1. Let Dirs=node_modules_paths (START)
2. For each DIR in DIRS:
A. Load_as_file (dir/x)
B. Load_as_directory (dir/x)
Node_modules_paths (START)
1. Let PARTS = Path Split (START)
2. Let I = Count of PARTS-1
3. Let DIRS = []
4. While I >= 0,
A. If parts[i] = "Node_modules" CONTINUE
B. DIR = Path Join (parts[0. I] + "Node_modules")
C. DIRS = DIRS + DIR
D. Let I = I-1
5. Return DIRS


Content from: Http://www.runoob.com/nodejs/nodejs-module-system.html + My understanding

10. 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.