module APImodule API#Module object#Provides general utility methods when interacting with instances ofModule, themodule variable often seen inCommonJS modules. Accessedviaimport 'module' orrequire('module').
module.builtinModules#A list of the names of all modules provided by Node.js. Can be used to verifyif a module is maintained by a third party or not.
module in this context isn't the same object that's providedby themodule wrapper. To access it, require theModule module:
// module.mjs// In an ECMAScript moduleimport { builtinModulesas builtin }from'module';// module.cjs// In a CommonJS moduleconst builtin =require('module').builtinModules;
module.createRequire(filename)#filename<string> |<URL> Filename to be used to construct the requirefunction. Must be a file URL object, file URL string, or absolute pathstring.import { createRequire }from'module';constrequire =createRequire(import.meta.url);// sibling-module.js is a CommonJS module.const siblingModule =require('./sibling-module');module.createRequireFromPath(filename)#createRequire() instead.filename<string> Filename to be used to construct the relative requirefunction.const { createRequireFromPath } =require('module');const requireUtil =createRequireFromPath('../src/utils/');// Require `../src/utils/some-tool`requireUtil('./some-tool');module.syncBuiltinESMExports()#Themodule.syncBuiltinESMExports() method updates all the live bindings forbuiltinES Modules to match the properties of theCommonJS exports. Itdoes not add or remove exported names from theES Modules.
const fs =require('fs');const assert =require('assert');const { syncBuiltinESMExports } =require('module');fs.readFile = newAPI;delete fs.readFileSync;functionnewAPI() {// ...}fs.newAPI = newAPI;syncBuiltinESMExports();import('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);});Helpers for interacting with the source map cache. This cache ispopulated 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 moduleimport { findSourceMap,SourceMap }from'module';// module.cjs// In a CommonJS moduleconst { findSourceMap,SourceMap } =require('module');
module.findSourceMap(path)#path<string>path is the resolved path for the file for which a corresponding source mapshould be fetched.
module.SourceMap#new SourceMap(payload)#payload<Object>Creates a newsourceMap instance.
payload is an object with keys matching theSource map v3 format:
file:<string>version:<number>sources:<string[]>sourcesContent:<string[]>names:<string[]>mappings:<string>sourceRoot:<string>sourceMap.payload#Getter for the payload used to construct theSourceMap instance.
sourceMap.findEntry(lineNumber, columnNumber)#Given a line number and column number in the generated source file, returnsan object representing the position in the original file. The object returnedconsists of the following keys: