Modules: node:module API | Node.js v18.17.1 Documentation

Node.js

- About this documentation
- Usage and example

- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules: `node:module` API
- Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib

- Code repository and issue tracker

Table of contents

- Modules: `node:module` API
  - The `Module` object
    - `module.builtinModules`
    - `module.createRequire(filename)`
    - `module.isBuiltin(moduleName)`
    - `module.syncBuiltinESMExports()`
  - Source map v3 support
    - `module.findSourceMap(path)`
    - Class: `module.SourceMap`
      - `new SourceMap(payload)`
      - `sourceMap.payload`
      - `sourceMap.findEntry(lineNumber, columnNumber)`

## Modules: `node:module` API#

Added in: v0.3.7

### The `Module` object#

- <Object>

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

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

- <string[]>

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

#### `module.createRequire(filename)`#

Added in: v12.2.0

- `filename` <string> | <URL> Filename to be used to construct the require function. Must be a file URL object, file URL string, or absolute path string.
- Returns: <require> Require function

    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)`#

Added in: v18.6.0

- `moduleName` <string> name of the module
- Returns: <boolean> returns true if the module is builtin else returns false

    import { isBuiltin } from 'node:module';
    isBuiltin('node:fs'); // true
    isBuiltin('fs'); // true
    isBuiltin('wss'); // false

#### `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('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);
    });

### Source map v3 support#

Added in: v13.7.0, v12.17.0

Stability: 1 - Experimental

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 'node:module';// module.cjs
    // In a CommonJS module
    const { findSourceMap, SourceMap } = require('node:module');

#### `module.findSourceMap(path)`#

Added in: v13.7.0, v12.17.0

- `path` <string>
- Returns: <module.SourceMap> | <undefined> Returns `module.SourceMap` if a source map is found, `undefined` otherwise.

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

#### Class: `module.SourceMap`#

Added in: v13.7.0, v12.17.0

##### `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)`#

- `lineNumber` <number>
- `columnNumber` <number>
- Returns: <Object>

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:

- generatedLine: <number>
- generatedColumn: <number>
- originalSource: <string>
- originalLine: <number>
- originalColumn: <number>
- name: <string>