- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
Node.js v18.12.1 documentation
- Node.js v18.12.1
-
► Table of contents
- Modules: CommonJS modules
- Enabling
Accessing the main module访问主模块- Package manager tips
- The
.mjs
extension - All together
- Caching
- Core modules
- Cycles
- File modules
- Folders as modules
- Loading from
node_modules
folders - Loading from the global folders
- The module wrapper
- The module scope
- The
module
object - The
Module
object - Source map v3 support
- Modules: CommonJS modules
-
► Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- ► Other versions
- ► Options
Table of contents
- Modules: CommonJS modules
- Enabling
- Accessing the main module
- Package manager tips
- The
.mjs
extension - All together
- Caching
- Core modules
- Cycles
- File modules
- Folders as modules
- Loading from
node_modules
folders - Loading from the global folders
- The module wrapper
- The module scope
- The
module
object - The
Module
object - Source map v3 support
Modules: CommonJS modules#
CommonJS modules are the original way to package JavaScript code for Node.js. CommonJS模块是为Node.js打包JavaScript代码的原始方法。Node.js also supports the ECMAScript modules standard used by browsers and other JavaScript runtimes.Node.js还支持浏览器和其他JavaScript运行时使用的ECMAScript模块标准。
In Node.js, each file is treated as a separate module. For example, consider a file named 在Node.js中,每个文件都被视为一个单独的模块。例如,考虑一个名为foo.js
:foo.js
的文件:
const circle = require('./circle.js');
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
On the first line, 在第一行,foo.js
loads the module circle.js
that is in the same directory as foo.js
.foo.js
加载与foo.js
位于同一目录中的模块circle.js
。
Here are the contents of 以下是circle.js
:circle.js
的内容:
const { PI } = Math;
exports.area = (r) => PI * r ** 2;
exports.circumference = (r) => 2 * PI * r;
The module circle.js
has exported the functions area()
and circumference()
. circle.js
模块已导出函数area()
和circference()
。Functions and objects are added to the root of a module by specifying additional properties on the special 通过在特殊导出对象上指定其他属性,可以将函数和对象添加到模块的根。exports
object.
Variables local to the module will be private, because the module is wrapped in a function by Node.js (see module wrapper). 模块的本地变量将是私有的,因为模块由Node.js封装在函数中(参见模块包装器)。In this example, the variable 在本例中,变量PI
is private to circle.js
.PI
是circle.js
的私有变量。
The 可以为module.exports
property can be assigned a new value (such as a function or object).module.exports
属性分配一个新值(例如函数或对象)。
Below, 下面,bar.js
makes use of the square
module, which exports a Square class:bar.js
使用了square
模块,该模块导出一个Square
类:
const Square = require('./square.js');
const mySquare = new Square(2);
console.log(`The area of mySquare is ${mySquare.area()}`);
The square
module is defined in square.js
:square
模块定义为square.js
:
// Assigning to exports will not modify module, must use module.exports
module.exports = class Square {
constructor(width) {
this.width = width;
}
area() {
return this.width ** 2;
}
};
The CommonJS module system is implemented in the CommonJS模块系统在module
core module.module
核心模块中实现。