- 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 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v16.20.2 文档
- Node.js v16.20.2
- 目录
-
导航
- 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 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
模块:node:module API#>
【Modules: node:module API】
Module 对象#>
【The Module object】
在与 Module 实例交互时提供通用实用方法,这是在 CommonJS 模块中经常看到的 module 变量。可以通过 import 'node:module' 或 require('node:module') 访问。
【Provides general utility methods when interacting with instances of
Module, the module variable often seen in CommonJS modules. Accessed
via import 'node:module' or require('node:module').】
module.builtinModules#>
Node.js 提供的所有模块名称列表。可用于验证某个模块是否由第三方维护。
【A list of the names of all modules provided by Node.js. Can be used to verify if a module is maintained by a third party or not.】
在此上下文中,module 并不是 模块封装 提供的同一个对象。要访问它,需要引入 Module 模块:
// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'node:module';// module.cjs
// In a CommonJS module
const builtin = require('node:module').builtinModules;
module.createRequire(filename)#>
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
// sibling-module.js is a CommonJS module.
const siblingModule = require('./sibling-module');
module.isBuiltin(moduleName)#>
import { isBuiltin } from 'node:module';
isBuiltin('node:fs'); // true
isBuiltin('fs'); // true
isBuiltin('wss'); // false
module.syncBuiltinESMExports()#>
module.syncBuiltinESMExports() 方法会更新内置 ES 模块 的所有实时绑定,以匹配 CommonJS 导出的属性。它不会添加或删除 ES 模块 的导出名称。
【The module.syncBuiltinESMExports() method updates all the live bindings for
builtin ES Modules to match the properties of the CommonJS exports. It
does not add or remove exported names from the ES Modules.】
const fs = require('node:fs');
const assert = require('node:assert');
const { syncBuiltinESMExports } = require('node:module');
fs.readFile = newAPI;
delete fs.readFileSync;
function newAPI() {
// ...
}
fs.newAPI = newAPI;
syncBuiltinESMExports();
import('node:fs').then((esmFS) => {
// It syncs the existing readFile property with the new value
assert.strictEqual(esmFS.readFile, newAPI);
// readFileSync has been deleted from the required fs
assert.strictEqual('readFileSync' in fs, false);
// syncBuiltinESMExports() does not remove readFileSync from esmFS
assert.strictEqual('readFileSync' in esmFS, true);
// syncBuiltinESMExports() does not add names
assert.strictEqual(esmFS.newAPI, undefined);
});
源映射 v3 支持#>
【Source map v3 support】
用于与源映射缓存交互的辅助工具。当启用源映射解析并且在模块的页脚中找到 源映射包含指令 时,此缓存会被填充。
【Helpers for interacting with the source map cache. This cache is populated when source map parsing is enabled and source map include directives are found in a modules' footer.】
要启用源映射解析,必须使用标志 --enable-source-maps 运行 Node.js,或者通过设置 NODE_V8_COVERAGE=dir 启用代码覆盖率。
【To enable source map parsing, Node.js must be run with the flag
--enable-source-maps, or with code coverage enabled by setting
NODE_V8_COVERAGE=dir.】
// module.mjs
// In an ECMAScript module
import { findSourceMap, SourceMap } from 'node:module';// module.cjs
// In a CommonJS module
const { findSourceMap, SourceMap } = require('node:module');
module.findSourceMap(path)#>
path<string>- 返回: <module.SourceMap>
path 是文件的解析路径,应为该文件获取相应的源映射。
类:module.SourceMap#>
【Class: module.SourceMap】
new SourceMap(payload)#>
payload<Object>
创建一个新的 sourceMap 实例。
【Creates a new sourceMap instance.】
payload 是一个对象,其键与 源映射 v3 格式 匹配:
file: <string>version: <number>- “来源”:<string[]>
sourcesContent: 字符串数组[]- '名字':<string[]>
mappings: <string>sourceRoot: <string>
sourceMap.payload#>
- 返回:<Object>
用于构造 SourceMap 实例的有效负载的获取方法。
【Getter for the payload used to construct the SourceMap instance.】
sourceMap.findEntry(lineNumber, columnNumber)#>
给定生成的源文件中的行号和列号,返回表示原始文件中位置的对象。返回的对象包含以下键:
【Given a line number and column number in the generated source file, returns an object representing the position in the original file. The object returned consists of the following keys:】