Modules: module API | Node.js v12.22.12 Documentation (original) (raw)

Modules: module API#

The Module object#

Provides general utility methods when interacting with instances ofModule, the module variable often seen in CommonJS modules. Accessed via import 'module' or require('module').

module.builtinModules#

Added in: v9.3.0, v8.10.0, v6.13.0

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 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 'module';
// module.cjs
// In a CommonJS module
const builtin = require('module').builtinModules;

module.createRequire(filename)#

Added in: v12.2.0

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

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

module.createRequireFromPath(filename)#

Added in: v10.12.0Deprecated since: v12.2.0

const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils/');

// Require `../src/utils/some-tool`
requireUtil('./some-tool');

module.syncBuiltinESMExports()#

Added in: v12.12.0

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 support#

Added in: v13.7.0, v12.17.0

Helpers for interacting with the source map cache. This cache is populated when source map parsing is enabled andsource 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 settingNODE_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])#

Added in: v13.7.0, v12.17.0

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 findSourceMapin exceptional flows, such as when an overriddenError.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.

Class: module.SourceMap#

Added in: v13.7.0, v12.17.0

new SourceMap(payload)#

Creates a new sourceMap instance.

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

sourceMap.payload#

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: