- assert - 断言
- async_hooks - 异步钩子
- Buffer - 缓冲器
- child_process - 子进程
- cluster - 集群
- console - 控制台
- crypto - 加密
- debugger - 调试器
- dgram - 数据报
- dns - 域名服务器
- domain - 域
- Error - 错误
- events - 事件触发器
- fs - 文件系统
- global - 全局变量
- http - HTTP
- http2 - HTTP/2
- https - HTTPS
- inspector - 检查器
- module - 模块
- net - 网络
- os - 操作系统
- path - 路径
- perf_hooks - 性能钩子
- process - 进程
- punycode - 域名代码
- querystring - 查询字符串
- readline - 逐行读取
- repl - 交互式解释器
- stream - 流
- string_decoder - 字符串解码器
- timer - 定时器
- tls - 安全传输层
- trace_events - 跟踪事件
- tty - 终端
- url - URL
- util - 实用工具
- v8 - V8引擎
- vm - 虚拟机
- wasi - WASI
- worker_threads - 工作线程
- zlib - 压缩
目录
module(模块)#
Module 对象#
当与 Module
实例(module
变量常见于 CommonJS 模块中)进行交互时,提供通用的工具方法。
通过 import 'module'
或 require('module')
访问。
module.builtinModules
#
罗列 Node.js 提供的所有模块名称。可以用来判断模块是否为第三方所维护。
注意, module
在此处含义与模块封装器所提供的 module
是不同的。可以通过引入 Module
模块访问:
// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'module';
// module.cjs
// In a CommonJS module
const builtin = require('module').builtinModules;
module.createRequire(filename)
#
filename
<string> | <URL> 用于构造 require 函数的文件名。必须是一个文件 URL 对象、文件 URL 字符串、或绝对路径字符串。- 返回: <require> require 函数。
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// sibling-module.js 是一个 CommonJS 模块。
const siblingModule = require('./sibling-module');
module.createRequireFromPath(filename)
#
createRequire()
。filename
<string> Filename to be used to construct the relative require function.- Returns: <require> Require function
const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils/');
// Require `../src/utils/some-tool`
requireUtil('./some-tool');
module.syncBuiltinESMExports()
#
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('fs');
const { syncBuiltinESMExports } = require('module');
fs.readFile = null;
delete fs.readFileSync;
fs.newAPI = function newAPI() {
// ...
};
syncBuiltinESMExports();
import('fs').then((esmFS) => {
assert.strictEqual(esmFS.readFile, null);
assert.strictEqual('readFileSync' in fs, true);
assert.strictEqual(esmFS.newAPI, undefined);
});
Source Map V3 的支持#
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.
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 'module';
// module.cjs
// In a CommonJS module
const { findSourceMap, SourceMap } = require('module');
module.findSourceMap(path[, error])
#
path
<string>error
<Error>- Returns: <module.SourceMap>
path
is the resolved path for the file for which a corresponding source map
should be fetched.
The error
instance should be passed as the second parameter to findSourceMap
in exceptional flows, such as when an overridden
Error.prepareStackTrace(error, trace)
is invoked. Modules are not added to
the module cache until they are successfully loaded. In these cases, source maps
are associated with the error
instance along with the path
.
module.SourceMap 类#
new SourceMap(payload)
#
payload
<Object>
Creates a new sourceMap
instance.
payload
is an object with keys matching the Source map v3 format:
file
: <string>version
: <number>sources
: <string[]>sourcesContent
: <string[]>names
: <string[]>mappings
: <string>sourceRoot
: <string>
sourceMap.payload
#
- Returns: <Object>
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: