Modules#
Stability: 5 - Locked
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 add functions and objects to the root of your module, you can add them to the special exports
object.
Variables local to the module will be private, as though the module was wrapped in a function. In this example the variable PI
is private to circle.js
.
If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports
instead of exports
.
Below, bar.js
makes use of the square
module, which exports a constructor:
var square = require('./square.js');
var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());
The square
module is defined in square.js
:
// assigning to exports will not modify module, must use module.exports
module.exports = function(width) {
return {
area: function() {
return width * width;
}
};
}
The module system is implemented in the require("module")
module.
Cycles#
When there are circular require()
calls, a module might not have finished executing when it is returned.
Consider this situation:
a.js
:
console.log('a starting');
exports.done = false;
var b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
b.js
:
console.log('b starting');
exports.done = false;
var a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
main.js
:
console.log('main starting');
var a = require('./a.js');
var b = require('./b.js');
console.log('in main, a.done=%j, b.done=%j', a.done, b.done);
When main.js
loads a.js
, then a.js
in turn loads b.js
. At that point, b.js
tries to load a.js
. In order to prevent an infinite loop, an unfinished copy of the a.js
exports object is returned to the b.js
module. b.js
then finishes loading, and its exports
object is provided to the a.js
module.
By the time main.js
has loaded both modules, they're both finished. The output of this program would thus be:
$ node main.js
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done=true, b.done=true
If you have cyclic module dependencies in your program, make sure to plan accordingly.
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
, .json
, and then .node
.
.js
files are interpreted as JavaScript text files, and .json
files are parsed as JSON text files. .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.
Without a leading '/' or './' to indicate a file, the module is either a "core module" or is loaded from a node_modules
folder.
If the given path does not exist, require()
will throw an Error with its code
property set to 'MODULE_NOT_FOUND'
.
Loading from node_modules
Folders#
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.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system 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/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
This allows programs to localize their dependencies, so that they do not clash.
You can require specific files or sub modules distributed with a module by including a path suffix after the module name. For instance require('example-module/path/to/file')
would resolve path/to/file
relative to where example-module
is located. The suffixed path follows the same module resolution semantics.
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.
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:
{ "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:
./some-library/index.js
./some-library/index.node
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.
Multiple calls to require('foo')
may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.
If you want to have a module execute code multiple times, then export a function, and call that function.
Module Caching Caveats#
Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules
folders), it is not a guarantee that require('foo')
will always return the exact same object, if it would resolve to different files.
The module
Object#
- {Object}
In each module, the module
free variable is a reference to the object representing the current module. For convenience,module.exports
is also accessible via the exports
module-global. module
isn't actually a global but rather local to each module.
module.exports#
- Object
The module.exports
object is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports
. Note that assigning the desired object toexports
will simply rebind the local exports
variable, which is probably not what you want to do.
For example suppose we were making a module called a.js
var EventEmitter = require('events').EventEmitter;
module.exports = new EventEmitter();
// Do some work, and after some time emit
// the 'ready' event from the module itself.
setTimeout(function() {
module.exports.emit('ready');
}, 1000);
Then in another file we could do
var a = require('./a');
a.on('ready', function() {
console.log('module a is ready');
});
Note that assignment to module.exports
must be done immediately. It cannot be done in any callbacks. This does not work:
x.js:
setTimeout(function() {
module.exports = { a: "hello" };
}, 0);
y.js:
var x = require('./x');
console.log(x.a);
exports alias#
The exports
variable that is available within a module starts as a reference to module.exports
. As with any variable, if you assign a new value to it, it is no longer bound to the previous value.
To illustrate the behaviour, imagine this hypothetical implementation of require()
:
function require(...) {
// ...
function (module, exports) {
// Your module code here
exports = some_func; // re-assigns exports, exports is no longer
// a shortcut, and nothing is exported.
module.exports = some_func; // makes your module export 0
} (module, module.exports);
return module;
}
As a guideline, if the relationship between exports
and module.exports
seems like magic to you, ignore exports
and only usemodule.exports
.
module.require(id)#
-
id
String - Return: Object
module.exports
from the resolved module
The module.require
method provides a way to load a module as if require()
was called from the original module.
Note that in order to do this, you must get a reference to the module
object. Since require()
returns the module.exports
, and the module
is typically only available within a specific module's code, it must be explicitly exported in order to be used.
module.id#
- String
The identifier for the module. Typically this is the fully resolved filename.
module.filename#
- String
The fully resolved filename to the module.
module.loaded#
- Boolean
Whether or not the module is done loading, or is in the process of loading.
module.parent#
- Module Object
The module that required this one.
module.children#
- Array
The module objects required by this one.
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:
require(X) from module at path Y
1. If X is a core module,
a. return the core module
b. STOP
2. If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"
LOAD_AS_FILE(X)
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.json is a file, parse X.json to a JavaScript Object. STOP
4. If X.node is a file, load X.node as binary addon. STOP
LOAD_AS_DIRECTORY(X)
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)
c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon. STOP
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
c. DIR = path join(PARTS[0 .. I] + "node_modules")
b. DIRS = DIRS + DIR
c. let I = I - 1
5. return DIRS
Loading from the global folders#
If the NODE_PATH
environment variable is set to a colon-delimited list of absolute paths, then node will search those paths for modules if they are not found elsewhere. (Note: On Windows, NODE_PATH
is delimited by semicolons instead of colons.)
Additionally, node will search in the following locations:
- 1:
$HOME/.node_modules
- 2:
$HOME/.node_libraries
- 3:
$PREFIX/lib/node
Where $HOME
is the user's home directory, and $PREFIX
is node's configured node_prefix
.
These are mostly for historic reasons. You are highly encouraged to place your dependencies locally in node_modules
folders. They will be loaded faster, and more reliably.
Accessing the main module#
When a file is run directly from Node, require.main
is set to its module
. That means that you can determine whether a file has been run directly by testing
require.main === module
For a file foo.js
, this will be true
if run via node foo.js
, but false
if run by require('./foo')
.
Because module
provides a filename
property (normally equivalent to __filename
), the entry point of the current application can be obtained by checking require.main.filename
.
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.
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 thenode_modules
folders as described above, this situation is very simple to resolve with the following architecture:
-
/usr/lib/node/foo/1.2.3/
- Contents of thefoo
package, version 1.2.3. -
/usr/lib/node/bar/4.3.2/
- Contents of thebar
package thatfoo
depends on. -
/usr/lib/node/foo/1.2.3/node_modules/bar
- Symbolic link to/usr/lib/node/bar/4.3.2/
. -
/usr/lib/node/bar/4.3.2/node_modules/*
- Symbolic links to the packages thatbar
depends on.
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.
相关推荐
6. **自定义模块加载路径**: 通过设置`NODE_PATH`环境变量,可以指定Node.js查找模块的额外路径。 7. **内置模块**: Node.js还提供了一些内置模块,如`fs`用于文件系统操作,`http`用于网络通信,无需`require`...
#### 三、Node.js模块加载机制 理解Node.js模块如何加载对于编写高效的应用程序至关重要。当Node.js执行一个JavaScript文件时,它会自动创建一个模块对象,然后将文件的内容包装在一个函数中。这个函数执行的结果...
`require` 函数根据提供的路径查找并加载模块,返回的是模块中 `module.exports` 或 `exports` 暴露的对象。 此外,`npm init` 是 Node.js 的包管理器 npm 提供的一个命令,用于生成 `package.json` 文件。这个文件...
而文件模块则是用户自己编写的 JavaScript 文件,它们需要在运行时动态加载,虽然加载速度相对较慢,但灵活性更高,可以满足各种自定义需求。 在 Node.js 中创建自定义模块,你需要将常用功能封装到独立的 ...
Motto提供了类似于Nodejs的模块环境,可以在golang中运行javascript文件。 安装 go get github.com/ddliu/motto 用法 var _ = require ( 'underscore' ) ; var data = require ( './data.json' ) ; // [3, 2, 1, 4, ...
// 网页加载完成后的操作 }); }); ``` 4. **交互与捕获**: 你可以使用`page`对象的方法如`evaluate`来在页面上下文中执行JavaScript代码,获取网页内容或者执行页面操作。例如,获取页面标题: ```javascript ...
RequireJS的核心理念是通过异步加载(Asynchronous Module Definition, AMD)来实现模块化,使得开发者可以将复杂的代码结构分解成可重用、可维护的小模块,提升了开发效率和代码质量。 在浏览器端(客户端),...
它加载所有基本的 nodejs 模块并加载 watcher、log、uncaught 和 loaderp 分支(模块和分支之间存在差异)watcher2.js 它的目的很简单,它重新加载需要的模块并观察它的变化,然后重新加载它。日志文件简单地登录...
nodejs模块获取磁盘信息,将调用系统命令获取驱动器信息。 解析结果并在数组中加载信息。 用法 var d = require('diskinfo'); d.getDrives(function(err, aDrives) { for (var i = 0; i < aDrives.length; i++)...
模块化的好处是避免命名冲突(减少命名空间污染),更好的分离、按需加载、更高复用性、高可维护性。 NodeJS 中的模块使用 exports 对象来向外暴露成员,而 require 函数用来引入模块。exports 是一个对象,...
- `require`: 这是 NodeJS 中加载模块的关键方法,这里我们加载了内置的 `http` 和 `util` 模块。`http` 模块用于创建 HTTP 服务器,`util` 模块提供了一些实用工具函数,如 `puts` 用于在控制台打印信息。 - `...
在循环引用的情况下,被引用的模块会被加载一次,但是由于其依赖的模块尚未加载完毕,因此被引用的模块只会执行到其所有依赖模块加载前的部分。Node.js会保证模块不会被重复加载,这避免了无限循环的问题。循环引用...
NodeAsp使用遵循CommonJS规范的require,完全兼容NodeJS模块加载方式,让您可以直接使用NodeJS 50%以上的模块。一切不关乎NodeJS运行环境和ES5-ES6特有对象的模块都能直接使用。如此庞大的模块资源库,这在以往任何...
modJS 是一套的前端模块加载解决方案。与传统的模块加载相比,modJS 会根据产品实际使用场景,自动选择一种相应的方案,使最终的实现非常轻量简洁。 使用 模块的定义 modJS是一个精简版的AMD/CMD规范,并不完全...
下面将详细介绍NodeJS模块系统和ES6模块系统,并指出在使用过程中的注意点。 ### NodeJS模块系统 NodeJS最开始使用的是CommonJS模块规范,该规范由社区提出并广泛使用于NodeJS服务端开发中。CommonJS模块规范中...
也就是一个入口的js文件,里面加入一些特定的功能,teacher.js.2 导出模块,把这个功能和这个文件也就是这个模块名建立关系 exports.add = function() {}3 加载模块,在其他文件里引入并加载这个文件,在Nodejs中...
NodeAsp使用遵循CommonJS规范的require,完全兼容NodeJS模块加载方式,让您可以直接使用NodeJS 50%以上的模块。一切不关乎NodeJS运行环境和ES5-ES6特有对象的模块都能直接使用。如此庞大的模块资源库,这在以往任何...
在JavaScript代码中,我们可以使用`require`来加载这个模块: ```javascript const addon = require('./build/Release/addon'); console.log(addon.add(1, 2)); // 输出:3 ``` 至此,我们已经完成了一个简单的...
一个简单的cli工具,用于试用不同的nodejs模块。 安装 npm install -g trymodule 用法 trymodule colors 如果需要,下载模块颜色,并使用当前范围内加载的颜色启动nodejs REPL,以供您使用。 trymodule colors ...
JavaScript 和 Node.js 模块是Web开发中的重要组成部分,它们为开发者提供了强大的工具来构建复杂的交互式网页应用。本主题将深入探讨如何使用JavaScript和Node.js来创建针对特定用户代理的polyfill包。 首先,理解...