Home  >  Article  >  Web Front-end  >  Detailed explanation of Node.js module loading

Detailed explanation of Node.js module loading

高洛峰
高洛峰Original
2016-12-26 09:32:441431browse

JavaScript is one of the most frequently used programming languages ​​in the world. It is the universal language of the Web world and is used by all browsers. The birth of JavaScript can be traced back to the Netscape era. Its core content was hastily developed to compete with Microsoft and participate in the fierce browser war at that time. Due to its premature release, it inevitably resulted in some not-so-good features.

Despite its short development time, JavaScript still has many powerful features, except for the fact that each script shares a global namespace.

Once a web page loads JavaScript code, it will be injected into the global namespace and will share the same address space with all other loaded scripts. This will lead to many security issues, conflicts, and some common problems, such as bugs. Both difficult to track and difficult to resolve.

But thankfully, Node has set some specifications for server-side JavaScript and also implemented the CommonJS module standard. In this standard, each module has its own context and is distinguished from other modules. This means that modules will not pollute the global scope, because there is no such thing as a global scope, and modules will not interfere with each other.

In this chapter, we will learn about several different modules and how to load them.

Splitting your code into a series of well-defined modules can help you take control of your application. Below we will learn how to create and use your own modules.

Understand how Node loads modules

In Node, you can reference modules through file paths or module names. If you reference a non-core module by name, Node will eventually map the module name to the corresponding module file path. Those core modules that contain core functions will be preloaded when Node starts.

Non-core modules include third-party modules installed using NPM (Node Package Manager), as well as local modules created by you or your colleagues.

Each module imported by the current script will expose a set of public APIs to programmers. Before using the module, you need to use the require function to import it, like this:

var module = require(‘module_name')

The above code will import a module named module_name, It may be a core module or a module installed using NPM. The require function returns an object containing all the public APIs of the module. Depending on the module, the returned object may be any JavaScript value, a function, or an object containing a series of properties (a function, an array, or any JavaScript object).

Export module

The CommonJS module system is the only way to share objects and functions between files under Node. For a very complex program, you should restructure some classes, objects or functions into a series of well-defined reusable modules. To the user of the module, the module exposes only the code you specify.

In the following example, you will learn that there is a one-to-one correspondence between files and modules in Node. We created a file called circle.js, which only exports the Circle constructor.

function Circle(x, y, r) {
       function r_squared() {
              return Math.pow(r, 2);
       }
       function area() {
              return Math.PI * r_squared();
       }
       return {area: area};
}
module.exports = Circle;

The most important line in the code is the last line, which defines what the module exports. module is a special variable, which represents the current module itself, and module.exports is the object exported by the module. It can be any object. In this example, we exported the constructor of Circle so that module users can use This module creates Circle instances.

You can also export some complex objects. module.exports is initialized as an empty object. You can export any content you want to expose to the outside world as attributes of the module.exports object. For example, you design a module that exposes a set of functions:

                  function printA() {
         console.log('A');
}
function printB() {
         console.log('B');
}
function printC() {
         console.log('C');
}
module.exports.printA = printA;
module.exports.printB = printB;
module.exports.pi = Math.PI;

This module exports two functions (printA and printB) and a number (pi). The calling code looks like this:

var myModule2 = require('./myModule2');
myModule2.printA(); // -> A
myModule2.printB(); // -> B
console.log(myModule2.pi); // -> 3.141592653589793

Load module

As mentioned before, you can use the require function to load modules. You don’t have to worry about calling require in the code will affect the global namespace, because there is no concept of global namespace in Node. If the module exists and there are no syntax or initialization errors, the require function will return the module object, and you can assign this object to any local variable.

There are several different types of modules, which can be roughly divided into core modules, local modules and third-party modules installed through NPM. According to the type of module, there are several ways to reference the module. Let's learn about this knowledge.

Loading core modules

Node has some modules that are compiled into binary files, called core modules. They cannot be referenced by paths, only by module names. The core module has the highest loading priority. Even if there is a third-party module with the same name, the core module will be loaded first.

For example, if you want to load and use the http core module, you can do this:

         var http = require('http');

This will return an http module object that contains the API of those htpp modules defined in the Node API documentation.

Loading file modules

You can also use an absolute path to load a module from the file system:

var myModule = require('/home/pedro/my_modules/my_module');

You can also use a relative path based on the current file:

var myModule = require('../my_modules/my_module');

var myModule2 = require('./lib/my_module_2');

注意上面的代码,你可以省略文件名的扩展名,如果Node找不到这个文件,会尝试在文件名后加上js后缀再次查找(译者注:其实除了js,还会查找json和node,具体可以看官网文档),因此,如果在当前目录下存在一个叫my_module.js的文件,会有下面两种加载方式:

var myModule = require('./my_module');
var myModule = require('./my_module.js');

加载目录模块

你还可以使用目录的路径来加载模块:

var myModule = require('./myModuleDir');

Node会假定这个目录是个模块包,并尝试在这个目录下搜索包定义文件package.json。

如果没找到,Node会假设包的入口点是index.js文件(译者注:除了index.js还会查找index.node,.node文件是Node的二进制扩展包,具体见官方文档),以上面代码为例,Node会尝试查找./myModuleDir/index.js文件。

反之,如果找到了package.json文件,Node会尝试解析它,并查找包定义里的main属性,然后把main属性的值当作入口点的相对路径。以本例来说,如果package.json定义如下:

                   {
                            "name" : "myModule",
                            "main" : "./lib/myModule.js"
                   }

Node就会尝试加载./myModuleDir/lib/myModule.js文件

从node_modules目录加载

如果require函数的参数不是相对路径,也不是核心模块名,Node会在当前目录的node_modules子目录下查找,比如下面的代码,Node会尝试查找文件./node_modules/myModule.js:

var myModule = require('myModule.js');

如果没找到,Node会继续在上级目录的node_modules文件夹下查找,如果还没找到就继续向上层目录查找,直到找到对应的模块或者到达根目录。


你可以使用这个特性来管理node_modules目录的内容或模块,不过最好还是把模块的管理任务交给NPM(见第一章),本地node_modules目录是NPM安装模块的默认位置,这个设计把Node和NPM关联在了一起。通常,作为开发人员不必太关心这个特性,你可以简单的使用NPM安装,更新和删除包,它会帮你维护node_modules目录

缓存模块

模块在第一次成功加载后会被缓存起来,就是说,如果模块名被解析到同一个文件路径,那么每次调用require(‘myModule')都确切地会返回同一个模块。

比如,有一个叫my_module.js的模块,包含下面的内容:

console.log('module my_module initializing...');
module.exports = function() {
         console.log('Hi!');
};
console.log('my_module initialized.');

然后用下面的代码加载这个模块:

var myModuleInstance1 = require('./my_module');

它会产生下面的输出:

module my_module initializing...
my_module initialized

如果我们两次导入它:

var myModuleInstance1 = require('./my_module');
var myModuleInstance2 = require('./my_module');

如果我们两次导入它:

var myModuleInstance1 = require('./my_module');
var myModuleInstance2 = require('./my_module');

输出依然是:

module my_module initializing...
my_module initialized

也就是说,模块的初始化代码仅执行了一次。当你构建自己的模块时,如果模块的初始化代码里含有可能产生副作用的代码,一定要特别注意这个特性。

小结

Node取消了JavaScript的默认全局作用域,转而采用CommonJS模块系统,这样你可以更好的组织你的代码,也因此避免了很多安全问题和bug。可以使用require函数来加载核心模块,第三方模块,或从文件及目录加载你自己的模块

还可以用相对路径或者绝对路径来加载非核心模块,如果把模块放到了node_modules目录下或者对于用NPM安装的模块,你还可以直接使用模块名来加载。

 译者注:

建议读者把官方文档的模块章节阅读一遍,个人感觉比作者讲得更清晰明了,而且还附加了一个非常具有代表性的例子,对理解Node模块加载会很有很大帮助。下面把那个例子也引用过来:

用require(X) 加载路径Y下的模块
1. 如果X是核心模块,
   a. 加载并返回核心模块
   b. 结束
2. 如果X以 './' or '/' or '../ 开始'
   a. LOAD_AS_FILE(Y + X)
   b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. 抛出异常:"not found"
LOAD_AS_FILE(X)
1. 如果X是个文件,把 X作为JavaScript 脚本加载,加载完毕后结束
2. 如果X.js是个文件,把X.js 作为JavaScript 脚本加载,加载完毕后结束
3. 如果X.node是个文件,把X.node 作为Node二进制插件加载,加载完毕后结束
LOAD_AS_DIRECTORY(X)
1. 如果 X/package.json文件存在,
   a. 解析X/package.json, 并查找 "main"字段.
   b. 另M = X + (main字段的值)
   c. LOAD_AS_FILE(M)
2. 如果X/index.js文件存在,把 X/index.js作为JavaScript 脚本加载,加载完毕后结束
3. 如果X/index.node文件存在,把load X/index.node作为Node二进制插件加载,加载完毕后结束
LOAD_NODE_MODULES(X, START)
1. 另DIRS=NODE_MODULES_PATHS(START)
2. 对DIRS下的每个目录DIR做如下操作:
   a. LOAD_AS_FILE(DIR/X)
   b. LOAD_AS_DIRECTORY(DIR/X)
NODE_MODULES_PATHS(START)
1. 另PARTS = path split(START)
2. 另ROOT = index of first instance of "node_modules" in PARTS, or 0
3. 另I = count of PARTS - 1
4. 另DIRS = []
5. while I > ROOT,
   a. 如果 PARTS[I] = "node_modules" 则继续后续操作,否则下次循环
   c. DIR = path join(PARTS[0 .. I] + "node_modules")
   b. DIRS = DIRS + DIR
   c. 另I = I - 1
6. 返回DIRS

更多Node.js模块加载详解相关文章请关注PHP中文网!

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