- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v18.20.0 文档
- Node.js v18.20.0
-
目录
- 全局对象
- 类:
AbortController
- 类:
Blob
- 类:
Buffer
- 类:
ByteLengthQueuingStrategy
__dirname
__filename
atob(data)
BroadcastChannel
btoa(data)
clearImmediate(immediateObject)
clearInterval(intervalObject)
clearTimeout(timeoutObject)
- 类:
CompressionStream
console
- 类:
CountQueuingStrategy
Crypto
crypto
CryptoKey
CustomEvent
- 类:
DecompressionStream
Event
EventTarget
exports
fetch
FormData
类global
Headers
类MessageChannel
MessageEvent
MessagePort
module
performance
process
queueMicrotask(callback)
- 类:
ReadableByteStreamController
- 类:
ReadableStream
- 类:
ReadableStreamBYOBReader
- 类:
ReadableStreamBYOBRequest
- 类:
ReadableStreamDefaultController
- 类:
ReadableStreamDefaultReader
require()
Response
Request
setImmediate(callback[, ...args])
setInterval(callback, delay[, ...args])
setTimeout(callback, delay[, ...args])
structuredClone(value[, options])
SubtleCrypto
DOMException
TextDecoder
- 类:
TextDecoderStream
TextEncoder
- 类:
TextEncoderStream
- 类:
TransformStream
- 类:
TransformStreamDefaultController
URL
URLSearchParams
WebAssembly
- 类:
WritableStream
- 类:
WritableStreamDefaultController
- 类:
WritableStreamDefaultWriter
- 类:
- 全局对象
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
全局对象#
¥Global objects
这些对象在所有模块中都可用。以下变量可能看起来是全局的,但实际上不是。它们只存在于模块作用域内,见 模块系统文档:
¥These objects are available in all modules. The following variables may appear to be global but are not. They exist only in the scope of modules, see the module system documentation:
此处列出的对象特定于 Node.js。内置对象 是 JavaScript 语言本身的一部分,也是全局可访问的。
¥The objects listed here are specific to Node.js. There are built-in objects that are part of the JavaScript language itself, which are also globally accessible.
类:AbortController
#
¥Class: AbortController
用于在选定的基于 Promise
的 API 中触发取消信号的实用工具类。该 API 基于 Web API AbortController
。
¥A utility class used to signal cancelation in selected Promise
-based APIs.
The API is based on the Web API AbortController
.
const ac = new AbortController();
ac.signal.addEventListener('abort', () => console.log('Aborted!'),
{ once: true });
ac.abort();
console.log(ac.signal.aborted); // Prints true
abortController.abort([reason])
#
-
reason
<any> 可选的原因,可在AbortSignal
的reason
属性中检索。¥
reason
<any> An optional reason, retrievable on theAbortSignal
'sreason
property.
触发中止信号,使 abortController.signal
触发 'abort'
事件。
¥Triggers the abort signal, causing the abortController.signal
to emit
the 'abort'
event.
abortController.signal
#
-
¥Type: <AbortSignal>
类:AbortSignal
#
¥Class: AbortSignal
-
¥Extends: <EventTarget>
AbortSignal
用于在调用 abortController.abort()
方法时通知监视器。
¥The AbortSignal
is used to notify observers when the
abortController.abort()
method is called.
静态方法:AbortSignal.abort([reason])
#
¥Static method: AbortSignal.abort([reason])
-
reason
:<any> -
¥Returns: <AbortSignal>
返回新的已中止的 AbortSignal
。
¥Returns a new already aborted AbortSignal
.
静态方法:AbortSignal.timeout(delay)
#
¥Static method: AbortSignal.timeout(delay)
-
delay
<number> 触发 AbortSignal 之前等待的毫秒数。¥
delay
<number> The number of milliseconds to wait before triggering the AbortSignal.
返回新的 AbortSignal
,其将在 delay
毫秒内中止。
¥Returns a new AbortSignal
which will be aborted in delay
milliseconds.
静态方法:AbortSignal.any(signals)
#
¥Static method: AbortSignal.any(signals)
-
signals
<AbortSignal[]> 其中的AbortSignal
组成了新的AbortSignal
。¥
signals
<AbortSignal[]> TheAbortSignal
s of which to compose a newAbortSignal
.
返回一个新的 AbortSignal
,如果任何提供的信号被中止,它将被中止。它的 abortSignal.reason
将设置为导致它中止的 signals
中的任何一个。
¥Returns a new AbortSignal
which will be aborted if any of the provided
signals are aborted. Its abortSignal.reason
will be set to whichever
one of the signals
caused it to be aborted.
事件:'abort'
#
¥Event: 'abort'
当调用 abortController.abort()
方法时,则触发 'abort'
事件。使用单个对象参数调用回调,该对象具有被设置为 'abort'
的 type
属性:
¥The 'abort'
event is emitted when the abortController.abort()
method
is called. The callback is invoked with a single object argument with a
single type
property set to 'abort'
:
const ac = new AbortController();
// Use either the onabort property...
ac.signal.onabort = () => console.log('aborted!');
// Or the EventTarget API...
ac.signal.addEventListener('abort', (event) => {
console.log(event.type); // Prints 'abort'
}, { once: true });
ac.abort();
与 AbortSignal
关联的 AbortController
只会触发一次 'abort'
事件。建议在添加 'abort'
事件监听器之前代码检查 abortSignal.aborted
属性是否为 false
。
¥The AbortController
with which the AbortSignal
is associated will only
ever trigger the 'abort'
event once. We recommended that code check
that the abortSignal.aborted
attribute is false
before adding an 'abort'
event listener.
任何绑定到 AbortSignal
的事件监听器都应使用 { once: true }
选项(或者,如果使用 EventEmitter
API 绑定监听器,则使用 once()
方法)以确保在处理 'abort'
事件后立即删除事件监听器。不这样做可能会导致内存泄漏。
¥Any event listeners attached to the AbortSignal
should use the
{ once: true }
option (or, if using the EventEmitter
APIs to attach a
listener, use the once()
method) to ensure that the event listener is
removed as soon as the 'abort'
event is handled. Failure to do so may
result in memory leaks.
abortSignal.aborted
#
-
类型:<boolean> 中止
AbortController
后为真。¥Type: <boolean> True after the
AbortController
has been aborted.
abortSignal.onabort
#
-
类型:<Function>
¥Type: <Function>
可选的回调函数,可以由用户代码设置,以便当调用 abortController.abort()
函数时得到通知。
¥An optional callback function that may be set by user code to be notified
when the abortController.abort()
function has been called.
abortSignal.reason
#
当触发 AbortSignal
时指定的可选的原因。
¥An optional reason specified when the AbortSignal
was triggered.
const ac = new AbortController();
ac.abort(new Error('boom!'));
console.log(ac.signal.reason); // Error: boom!
abortSignal.throwIfAborted()
#
如果 abortSignal.aborted
是 true
,则抛出 abortSignal.reason
。
¥If abortSignal.aborted
is true
, throws abortSignal.reason
.
类:Blob
#
¥Class: Blob
参见 <Blob>。
¥See <Blob>.
类:Buffer
#
¥Class: Buffer
用于处理二进制数据。参见 缓冲区。
¥Used to handle binary data. See the buffer section.
类:ByteLengthQueuingStrategy
#
¥Class: ByteLengthQueuingStrategy
¥Stability: 1 - Experimental.
ByteLengthQueuingStrategy
的浏览器兼容实现。
¥A browser-compatible implementation of ByteLengthQueuingStrategy
.
__dirname
#
此变量可能看起来是全局的,但实际上不是。参见 __dirname
。
¥This variable may appear to be global but is not. See __dirname
.
__filename
#
此变量可能看起来是全局的,但实际上不是。参见 __filename
。
¥This variable may appear to be global but is not. See __filename
.
atob(data)
#
Buffer.from(data, 'base64')
。¥Stability: 3 - Legacy. Use Buffer.from(data, 'base64')
instead.
buffer.atob()
的全局别名。
¥Global alias for buffer.atob()
.
BroadcastChannel
#
¥See <BroadcastChannel>.
btoa(data)
#
buffer.btoa()
的全局别名。
¥Global alias for buffer.btoa()
.
clearImmediate(immediateObject)
#
clearImmediate
在 定时器 部分中描述。
¥clearImmediate
is described in the timers section.
clearInterval(intervalObject)
#
clearInterval
在 定时器 部分中描述。
¥clearInterval
is described in the timers section.
clearTimeout(timeoutObject)
#
clearTimeout
在 定时器 部分中描述。
¥clearTimeout
is described in the timers section.
类:CompressionStream
#
¥Class: CompressionStream
¥Stability: 1 - Experimental.
CompressionStream
的浏览器兼容实现。
¥A browser-compatible implementation of CompressionStream
.
console
#
用于打印到标准输出和标准错误。参阅 console
章节。
¥Used to print to stdout and stderr. See the console
section.
类:CountQueuingStrategy
#
¥Class: CountQueuingStrategy
¥Stability: 1 - Experimental.
CountQueuingStrategy
的浏览器兼容实现。
¥A browser-compatible implementation of CountQueuingStrategy
.
Crypto
#
--experimental-global-webcrypto
CLI 标志启用此 API。¥Stability: 1 - Experimental. Enable this API with the
--experimental-global-webcrypto
CLI flag.
<Crypto> 的浏览器兼容实现。只有在编译 Node.js 二进制文件时包含对 node:crypto
模块的支持,此全局才可用。
¥A browser-compatible implementation of <Crypto>. This global is available
only if the Node.js binary was compiled with including support for the
node:crypto
module.
crypto
#
--experimental-global-webcrypto
CLI 标志启用此 API。¥Stability: 1 - Experimental. Enable this API with the
--experimental-global-webcrypto
CLI flag.
网络加密 API 的浏览器兼容实现。
¥A browser-compatible implementation of the Web Crypto API.
CryptoKey
#
--experimental-global-webcrypto
CLI 标志启用此 API。¥Stability: 1 - Experimental. Enable this API with the
--experimental-global-webcrypto
CLI flag.
<CryptoKey> 的浏览器兼容实现。只有在编译 Node.js 二进制文件时包含对 node:crypto
模块的支持,此全局才可用。
¥A browser-compatible implementation of <CryptoKey>. This global is available
only if the Node.js binary was compiled with including support for the
node:crypto
module.
CustomEvent
#
--experimental-global-customevent
CLI 标志启用此 API。¥Stability: 1 - Experimental. Enable this API with the
--experimental-global-customevent
CLI flag.
CustomEvent
网络应用接口 的浏览器兼容实现。
¥A browser-compatible implementation of the CustomEvent
Web API.
类:DecompressionStream
#
¥Class: DecompressionStream
¥Stability: 1 - Experimental.
DecompressionStream
的浏览器兼容实现。
¥A browser-compatible implementation of DecompressionStream
.
Event
#
Event
类的浏览器兼容的实现。有关详细信息,请参阅 EventTarget
和 Event
API。
¥A browser-compatible implementation of the Event
class. See
EventTarget
and Event
API for more details.
EventTarget
#
EventTarget
类的浏览器兼容的实现。有关详细信息,请参阅 EventTarget
和 Event
API。
¥A browser-compatible implementation of the EventTarget
class. See
EventTarget
and Event
API for more details.
exports
#
此变量可能看起来是全局的,但实际上不是。参见 exports
。
¥This variable may appear to be global but is not. See exports
.
fetch
#
--no-experimental-fetch
CLI 标志禁用此 API。¥Stability: 1 - Experimental. Disable this API with the --no-experimental-fetch
CLI flag.
fetch()
函数的浏览器兼容实现。
¥A browser-compatible implementation of the fetch()
function.
FormData
类#
¥Class FormData
--no-experimental-fetch
CLI 标志禁用此 API。¥Stability: 1 - Experimental. Disable this API with the --no-experimental-fetch
CLI flag.
<FormData> 的浏览器兼容实现。
¥A browser-compatible implementation of <FormData>.
global
#
在浏览器中,顶层作用域传统上是全局作用域。这意味着 var something
将定义一个新的全局变量,ECMAScript 模块除外。在 Node.js 中,这是不同的。顶层作用域不是全局作用域;Node.js 模块内的 var something
对于该模块而言是本地的,无论它是 CommonJS 模块 还是 ECMAScript 模块。
¥In browsers, the top-level scope has traditionally been the global scope. This
means that var something
will define a new global variable, except within
ECMAScript modules. In Node.js, this is different. The top-level scope is not
the global scope; var something
inside a Node.js module will be local to that
module, regardless of whether it is a CommonJS module or an
ECMAScript module.
Headers
类#
¥Class Headers
--no-experimental-fetch
CLI 标志禁用此 API。¥Stability: 1 - Experimental. Disable this API with the --no-experimental-fetch
CLI flag.
<Headers> 的浏览器兼容实现。
¥A browser-compatible implementation of <Headers>.
MessageChannel
#
MessageChannel
类。有关详细信息,请参阅 MessageChannel
。
¥The MessageChannel
class. See MessageChannel
for more details.
MessageEvent
#
MessageEvent
类。有关详细信息,请参阅 MessageEvent
。
¥The MessageEvent
class. See MessageEvent
for more details.
MessagePort
#
MessagePort
类。有关详细信息,请参阅 MessagePort
。
¥The MessagePort
class. See MessagePort
for more details.
module
#
此变量可能看起来是全局的,但实际上不是。参见 module
。
¥This variable may appear to be global but is not. See module
.
performance
#
¥The perf_hooks.performance
object.
process
#
进程对象。参阅 process
对象 章节。
¥The process object. See the process
object section.
queueMicrotask(callback)
#
-
callback
<Function> 要排队的函数。¥
callback
<Function> Function to be queued.
queueMicrotask()
方法将微任务排队以调用 callback
。如果 callback
抛出异常,将触发 process
对象 'uncaughtException'
事件。
¥The queueMicrotask()
method queues a microtask to invoke callback
. If
callback
throws an exception, the process
object 'uncaughtException'
event will be emitted.
微任务队列由 V8 管理,并且可以以类似于 process.nextTick()
队列的方式使用,后者由 Node.js 管理。在 Node.js 事件循环的每次轮询中,process.nextTick()
队列总是在微任务队列之前处理。
¥The microtask queue is managed by V8 and may be used in a similar manner to
the process.nextTick()
queue, which is managed by Node.js. The
process.nextTick()
queue is always processed before the microtask queue
within each turn of the Node.js event loop.
// Here, `queueMicrotask()` is used to ensure the 'load' event is always
// emitted asynchronously, and therefore consistently. Using
// `process.nextTick()` here would result in the 'load' event always emitting
// before any other promise jobs.
DataHandler.prototype.load = async function load(key) {
const hit = this._cache.get(key);
if (hit !== undefined) {
queueMicrotask(() => {
this.emit('load', hit);
});
return;
}
const data = await fetchData(key);
this._cache.set(key, data);
this.emit('load', data);
};
类:ReadableByteStreamController
#
¥Class: ReadableByteStreamController
¥Stability: 1 - Experimental.
ReadableByteStreamController
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableByteStreamController
.
类:ReadableStream
#
¥Class: ReadableStream
¥Stability: 1 - Experimental.
ReadableStream
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableStream
.
类:ReadableStreamBYOBReader
#
¥Class: ReadableStreamBYOBReader
¥Stability: 1 - Experimental.
ReadableStreamBYOBReader
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableStreamBYOBReader
.
类:ReadableStreamBYOBRequest
#
¥Class: ReadableStreamBYOBRequest
¥Stability: 1 - Experimental.
ReadableStreamBYOBRequest
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableStreamBYOBRequest
.
类:ReadableStreamDefaultController
#
¥Class: ReadableStreamDefaultController
¥Stability: 1 - Experimental.
ReadableStreamDefaultController
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableStreamDefaultController
.
类:ReadableStreamDefaultReader
#
¥Class: ReadableStreamDefaultReader
¥Stability: 1 - Experimental.
ReadableStreamDefaultReader
的浏览器兼容实现。
¥A browser-compatible implementation of ReadableStreamDefaultReader
.
require()
#
此变量可能看起来是全局的,但实际上不是。参见 require()
。
¥This variable may appear to be global but is not. See require()
.
Response
#
--no-experimental-fetch
CLI 标志禁用此 API。¥Stability: 1 - Experimental. Disable this API with the --no-experimental-fetch
CLI flag.
<Response> 的浏览器兼容实现。
¥A browser-compatible implementation of <Response>.
Request
#
--no-experimental-fetch
CLI 标志禁用此 API。¥Stability: 1 - Experimental. Disable this API with the --no-experimental-fetch
CLI flag.
<Request> 的浏览器兼容实现。
¥A browser-compatible implementation of <Request>.
setImmediate(callback[, ...args])
#
setImmediate
在 定时器 部分中描述。
¥setImmediate
is described in the timers section.
setInterval(callback, delay[, ...args])
#
setInterval
在 定时器 部分中描述。
¥setInterval
is described in the timers section.
setTimeout(callback, delay[, ...args])
#
setTimeout
在 定时器 部分中描述。
¥setTimeout
is described in the timers section.
structuredClone(value[, options])
#
WHATWG structuredClone
方法。
¥The WHATWG structuredClone
method.
SubtleCrypto
#
--experimental-global-webcrypto
CLI 标志启用此 API。¥Stability: 1 - Experimental. Enable this API with the
--experimental-global-webcrypto
CLI flag.
<SubtleCrypto> 的浏览器兼容实现。只有在编译 Node.js 二进制文件时包含对 node:crypto
模块的支持,此全局才可用。
¥A browser-compatible implementation of <SubtleCrypto>. This global is available
only if the Node.js binary was compiled with including support for the
node:crypto
module.
DOMException
#
WHATWG DOMException
类。有关详细信息,请参阅 DOMException
。
¥The WHATWG DOMException
class. See DOMException
for more details.
TextDecoder
#
WHATWG TextDecoder
类。参阅 TextDecoder
章节。
¥The WHATWG TextDecoder
class. See the TextDecoder
section.
类:TextDecoderStream
#
¥Class: TextDecoderStream
¥Stability: 1 - Experimental.
TextDecoderStream
的浏览器兼容实现。
¥A browser-compatible implementation of TextDecoderStream
.
TextEncoder
#
WHATWG TextEncoder
类。参阅 TextEncoder
章节。
¥The WHATWG TextEncoder
class. See the TextEncoder
section.
类:TextEncoderStream
#
¥Class: TextEncoderStream
¥Stability: 1 - Experimental.
TextEncoderStream
的浏览器兼容实现。
¥A browser-compatible implementation of TextEncoderStream
.
类:TransformStream
#
¥Class: TransformStream
¥Stability: 1 - Experimental.
TransformStream
的浏览器兼容实现。
¥A browser-compatible implementation of TransformStream
.
类:TransformStreamDefaultController
#
¥Class: TransformStreamDefaultController
¥Stability: 1 - Experimental.
TransformStreamDefaultController
的浏览器兼容实现。
¥A browser-compatible implementation of TransformStreamDefaultController
.
URL
#
WHATWG URL
类。参阅 URL
章节。
¥The WHATWG URL
class. See the URL
section.
URLSearchParams
#
WHATWG URLSearchParams
类。参阅 URLSearchParams
章节。
¥The WHATWG URLSearchParams
class. See the URLSearchParams
section.
WebAssembly
#
充当所有 W3C WebAssembly 相关功能的名称空间的对象。有关用法和兼容性,请参阅 Mozilla 开发者网络。
¥The object that acts as the namespace for all W3C WebAssembly related functionality. See the Mozilla Developer Network for usage and compatibility.
类:WritableStream
#
¥Class: WritableStream
¥Stability: 1 - Experimental.
WritableStream
的浏览器兼容实现。
¥A browser-compatible implementation of WritableStream
.
类:WritableStreamDefaultController
#
¥Class: WritableStreamDefaultController
¥Stability: 1 - Experimental.
WritableStreamDefaultController
的浏览器兼容实现。
¥A browser-compatible implementation of WritableStreamDefaultController
.
类:WritableStreamDefaultWriter
#
¥Class: WritableStreamDefaultWriter
¥Stability: 1 - Experimental.
WritableStreamDefaultWriter
的浏览器兼容实现。
¥A browser-compatible implementation of WritableStreamDefaultWriter
.