Node.js Manual & Documentation

Table Of Contents

Modules 模块

Node uses the CommonJS module system. Node has a simple module loading system. In Node, files and modules are in one-to-one correspondence. As an example, foo.js loads the module circle.js in the same directory.


The contents of foo.js:


var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is '
           + circle.area(4));

The contents of circle.js:


var PI = Math.PI;

exports.area = function (r) {
  return PI * r * r;

exports.circumference = function (r) {
  return 2 * PI * r;

The module circle.js has exported the functions area() and circumference(). To export an object, add to the special exports object.


Variables local to the module will be private. In this example the variable PI is private to circle.js.


Core Modules 核心模块

Node has several modules compiled into the binary. These modules are described in greater detail elsewhere in this documentation.


The core modules are defined in node's source in the lib/ folder.


Core modules are always preferentially loaded if their identifier is passed to require(). For instance, require('http') will always return the built in HTTP module, even if there is a file by that name.


File Modules 文件模块

If the exact filename is not found, then node will attempt to load the required filename with the added extension of .js, and then .node. .js files are interpreted as JavaScript text files, and .node files are interpreted as compiled addon modules loaded with dlopen.


A module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.


A module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

'./'为前缀的模块是指向文件的相对路径,相对于调用require()的文件。也就是说为了使require('./circle') 能找到正确的文件,circle.js必须位于与foo.js 相同的路径之下。

Without a leading '/' or './' to indicate a file, the module is either a "core module" or is loaded from a node_modules folder.

如果标明一个文件时没有 '/' 或 './'前缀,该模块或是"核心模块",或者位于 node_modules目录中。

Loading from `node_modules` Folders 从 `node_modules` 目录中加载

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

如果传递到 require()的模块标识符不是一个核心模块,并且不是以'/''../''./'开头,node将从当前模块的父目录开始,在其/node_modules子目录中加载该模块。

If it is not found there, then it moves to the parent directory, and so on, until either the module is found, or the root of the tree is reached.


For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

例如,如果在文件 '/home/ry/projects/foo.js'中调用 `require('bar.js'),node将会依次查找以下位置:

This allows programs to localize their dependencies, so that they do not clash.


Optimizations to the `node_modules` Lookup Process 优化 `node_modules` 的查找过程

When there are many levels of nested dependencies, it is possible for these file trees to get fairly long. The following optimizations are thus made to the process.


First, /node_modules is never appended to a folder already ending in /node_modules.

首先, /node_modules不要添加到以 /node_modules结尾的目录上。

Second, if the file calling require() is already inside a node_modules hierarchy, then the top-most node_modules folder is treated as the root of the search tree.


For example, if the file at '/home/ry/projects/foo/node_modules/bar/node_modules/baz/quux.js' called require('asdf.js'), then node would search the following locations:


Folders as Modules 目录作为模块

It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to require() as an argument.

很方便将程序或库组织成自包含的目录,并提供一个单独的入口指向那个库。有三种方式可以将一个子目录作为参数传递给 require()

The first is to create a package.json file in the root of the folder, which specifies a main module. An example package.json file might look like this:

第一种方法是在目录的根下创建一个名为package.json的文件,它指定了一个main 模块。一个package.jso文件的例子如下面所示:

{ "name" : "some-library",
  "main" : "./lib/some-library.js" }

If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.


This is the extent of Node's awareness of package.json files.


If there is no package.json file present in the directory, then node will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:

如果在目录中没有package.json文件,node将试图在该目录中加载index.jsindex.node文件。例如,在上面的例子中没有 package.json文件,require('./some-library')将试图加载:

Caching 缓存

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.


All Together... 总结一下...

To get the exact filename that will be loaded when require() is called, use the require.resolve() function.


Putting together all of the above, here is the high-level algorithm in pseudocode of what require.resolve does:


1. If X is a core module,
   a. return the core module
   b. STOP
2. If X begins with `./` or `/`,
   a. LOAD_AS_FILE(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"

1. If X is a file, load X as JavaScript text.  STOP
2. If X.js is a file, load X.js as JavaScript text.  STOP
3. If X.node is a file, load X.node as binary addon.  STOP

1. If X/package.json is a file,
   a. Parse X/package.json, and look for "main" field.
   b. let M = X + (json main field)
2. LOAD_AS_FILE(X/index)

2. for each DIR in DIRS:

1. let PARTS = path split(START)
2. let ROOT = index of first instance of "node_modules" in PARTS, or 0
3. let I = count of PARTS - 1
4. let DIRS = []
5. while I > ROOT,
   a. if PARTS[I] = "node_modules" CONTINUE
   c. DIR = path join(PARTS[0 .. I] + "node_modules")
   b. DIRS = DIRS + DIR
6. return DIRS

Loading from the `require.paths` Folders 从`require.paths`目录中加载

In node, require.paths is an array of strings that represent paths to be searched for modules when they are not prefixed with '/', './', or '../'. For example, if require.paths were set to:


[ '/home/micheil/.node_modules',
  '/usr/local/lib/node_modules' ]

Then calling require('bar/baz.js') would search the following locations:


The require.paths array can be mutated at run time to alter this behavior.


It is set initially from the NODE_PATH environment variable, which is a colon-delimited list of absolute paths. In the previous example, the NODE_PATH environment variable might have been set to:



Loading from the require.paths locations is only performed if the module could not be found using the node_modules algorithm above. Global modules are lower priority than bundled dependencies.


**Note:** Please Avoid Modifying `require.paths` **注意:** 请不要修改`requires.paths`

For compatibility reasons, require.paths is still given first priority in the module lookup process. However, it may disappear in a future release.


While it seemed like a good idea at the time, and enabled a lot of useful experimentation, in practice a mutable require.paths list is often a troublesome source of confusion and headaches.


Setting `require.paths` to some other value does nothing. 将`require.paths`设为其他值不会产生任何作用

This does not do what one might expect:


require.paths = [ '/usr/lib/node' ];

All that does is lose the reference to the actual node module lookup paths, and create a new reference to some other thing that isn't used for anything.


Putting relative paths in `require.paths` is... weird. 不建议在`require.paths`中发入相对路径

If you do this:



then it does not add the full resolved path to where ./lib is on the filesystem. Instead, it literally adds './lib', meaning that if you do require('y.js') in /a/b/x.js, then it'll look in /a/b/lib/y.js. If you then did require('y.js') in /l/m/n/o/p.js, then it'd look in /l/m/n/o/lib/y.js.


In practice, people have used this as an ad hoc way to bundle dependencies, but this technique is brittle.


Zero Isolation 零隔离

There is (by regrettable design), only one require.paths array used by all modules.


As a result, if one node program comes to rely on this behavior, it may permanently and subtly alter the behavior of all other node programs in the same process. As the application stack grows, we tend to assemble functionality, and it is a problem with those parts interact in ways that are difficult to predict.


Addenda: Package Manager Tips 附录:包管理技巧

The semantics of Node's require() function were designed to be general enough to support a number of sane directory structures. Package manager programs such as dpkg, rpm, and npm will hopefully find it possible to build native packages from Node modules without modification.

Node的require()函数的语义被设计的足够通用化,以支持各种常规目录结构。包管理程序如 dpkgrpmnpm将不用修改就能够从Node模块构建本地包。

Below we give a suggested directory structure that could work:


Let's say that we wanted to have the folder at /usr/lib/node/<some-package>/<some-version> hold the contents of a specific version of a package.


Packages can depend on one another. In order to install package foo, you may have to install a specific version of package bar. The bar package may itself have dependencies, and in some cases, these dependencies may even collide or form cycles.


Since Node looks up the realpath of any modules it loads (that is, resolves symlinks), and then looks for their dependencies in the node_modules folders as described above, this situation is very simple to resolve with the following architecture:


Thus, even if a cycle is encountered, or if there are dependency conflicts, every module will be able to get a version of its dependency that it can use.


When the code in the foo package does require('bar'), it will get the version that is symlinked into /usr/lib/node/foo/1.2.3/node_modules/bar. Then, when the code in the bar package calls require('quux'), it'll get the version that is symlinked into /usr/lib/node/bar/4.3.2/node_modules/quux.


Furthermore, to make the module lookup process even more optimal, rather than putting packages directly in /usr/lib/node, we could put them in /usr/lib/node_modules/<name>/<version>. Then node will not bother looking for missing dependencies in /usr/node_modules or /node_modules.


In order to make modules available to the node REPL, it might be useful to also add the /usr/lib/node_modules folder to the $NODE_PATH environment variable. Since the module lookups using node_modules folders are all relative, and based on the real path of the files making the calls to require(), the packages themselves can be anywhere.

为了使模块在node REPL中可用,你可能需要将/usr/lib/node_modules目录加入到$NODE_PATH环境变量中。由于在node_modules目录中搜索模块使用的是相对路径,基于调用require()的文件所在真实路径,因此包本身可以放在任何位置。