Node.js v16.20.2 文档


模块: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)#>

  • filename <string> | <URL> 用于构建 require 函数的文件名。必须是文件 URL 对象、文件 URL 字符串或绝对路径字符串。
  • 返回:<require> 需要函数
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)#>

  • moduleName <string> 模块的名称
  • 返回值:<boolean> 如果模块是内置的则返回 true,否则返回 false
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】

稳定性: 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.】

要启用源映射解析,必须使用标志 --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 是文件的解析路径,应为该文件获取相应的源映射。

类:module.SourceMap#>

【Class: module.SourceMap

new SourceMap(payload)#>

创建一个新的 sourceMap 实例。

【Creates a new sourceMap instance.】

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

sourceMap.payload#>

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

Node.js 中文网 - 粤ICP备13048890号