Node.js v16.20.1 文档

模块:node:module API#

Module 对象#

在与 Module 的实例交互时提供通用实用方法,module 变量经常出现在 CommonJS 模块中。 通过 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').


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 in this context isn't the same object that's provided by the module wrapper. To access it, require the 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;


  • filename <string> | <URL> 用于构造 require 函数的文件名。 必须是文件网址对象、文件网址字符串、或绝对路径字符串。
  • 返回: <require> require 函数
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

// sibling-module.js is a CommonJS module.
const siblingModule = require('./sibling-module'); 


  • moduleName <string> 模块名称
  • 返回: <boolean> 如果模块是内置的,则返回 true,否则返回 false
import { isBuiltin } from 'node:module';
isBuiltin('node:fs'); // true
isBuiltin('fs'); // true
isBuiltin('wss'); // false 


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;


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 支持#

稳定性: 1 - 实验

与源映射缓存交互的助手。 当启用源映射解析并且在模块的页脚中找到 源映射包含指令 时,将填充此缓存。

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.

要启用源映射解析,则 Node.js 必须使用标志 --enable-source-maps 运行、或者通过设置 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');


path 是文件的解析路径,应为其获取相应的源映射。

path is the resolved path for the file for which a corresponding source map should be fetched.


new SourceMap(payload)#

创建新的 sourceMap 实例。

Creates a new sourceMap instance.

payload 是一个对象,其键与 源映射 v3 格式 匹配:

payload is an object with keys matching the Source map v3 format:


用于构造 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: