The Module object
History
ObjectProvides 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
History
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): 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.findPackageJSON(specifier, base?): string | undefined
package.json to
retrieve. When passing a bare specifier, the package.json at the root of
the package is returned. When passing a relative specifier or an absolute specifier,
the closest parent package.json is returned.file: URL string or FS path) of the
containing module. For CJS, use __filename (not __dirname!); for ESM, use
import.meta.url. You do not need to pass it if specifier is an absolute specifier.Caveat: Do not use this to try to determine module format. There are many things affecting that determination; the
typefield of package.json is the least definitive (ex file extension supersedes it, and a loader hook supersedes that).
Caveat: This currently leverages only the built-in default resolver; if
resolvecustomization hooks are registered, they will not affect the resolution. This may change in the future.
/path/to/project ├ packages/ ├ bar/ ├ bar.js └ package.json // name = '@foo/bar' └ qux/ ├ node_modules/ └ some-package/ └ package.json // name = 'some-package' ├ qux.js └ package.json // name = '@foo/qux' ├ main.js └ package.json // name = '@foo'
// /path/to/project/packages/bar/bar.js import { findPackageJSON } from 'node:module'; findPackageJSON('..', import.meta.url); // '/path/to/project/package.json' // Same result when passing an absolute specifier instead: findPackageJSON(new URL('../', import.meta.url)); findPackageJSON(import.meta.resolve('../')); findPackageJSON('some-package', import.meta.url); // '/path/to/project/packages/bar/node_modules/some-package/package.json' // When passing an absolute specifier, you might get a different result if the // resolved module is inside a subfolder that has nested `package.json`. findPackageJSON(import.meta.resolve('some-package')); // '/path/to/project/packages/bar/node_modules/some-package/some-subfolder/package.json' findPackageJSON('@foo/qux', import.meta.url); // '/path/to/project/packages/qux/package.json'
// /path/to/project/packages/bar/bar.js const { findPackageJSON } = require('node:module'); const { pathToFileURL } = require('node:url'); const path = require('node:path'); findPackageJSON('..', __filename); // '/path/to/project/package.json' // Same result when passing an absolute specifier instead: findPackageJSON(pathToFileURL(path.join(__dirname, '..'))); findPackageJSON('some-package', __filename); // '/path/to/project/packages/bar/node_modules/some-package/package.json' // When passing an absolute specifier, you might get a different result if the // resolved module is inside a subfolder that has nested `package.json`. findPackageJSON(pathToFileURL(require.resolve('some-package'))); // '/path/to/project/packages/bar/node_modules/some-package/some-subfolder/package.json' findPackageJSON('@foo/qux', __filename); // '/path/to/project/packages/qux/package.json'
module.isBuiltin(moduleName): boolean
import { isBuiltin } from 'node:module'; isBuiltin('node:fs'); // true isBuiltin('fs'); // true isBuiltin('wss'); // false
module.register
History
--allow-worker.module.register(specifier, parentURL?, options?): void
module.registerHooks() instead.import(), except that if it is
relative, it is resolved relative to parentURL.specifier relative to a base
URL, such as import.meta.url, you can pass that URL here. Default:
'data:'Objectspecifier relative to a
base URL, such as import.meta.url, you can pass that URL here. This
property is ignored if the parentURL is supplied as the second argument.
Default: 'data:'anyinitialize hook.Object[]initialize hook.Register a module that exports hooks that customize Node.js module resolution and loading behavior. See Customization hooks.
This feature requires --allow-worker if used with the Permission Model.
module.registerHooks
History
module.registerHooks(options): Object
Register hooks that customize Node.js module resolution and loading behavior. See Customization hooks. The returned object can be used to deregister the hooks.
module.stripTypeScriptTypes
History
transform and sourceMap options.module.stripTypeScriptTypes(code, options?): string
module.stripTypeScriptTypes() removes type annotations from TypeScript code. It
can be used to strip type annotations from TypeScript code before running it
with vm.runInContext() or vm.compileFunction().
By default, it will throw an error if the code contains TypeScript features
that require transformation, such as enums. See type-stripping for more information.
WARNING: The output of this function should not be considered stable across Node.js versions, due to changes in the TypeScript parser.
import { stripTypeScriptTypes } from 'node:module'; const code = 'const a: number = 1;'; const strippedCode = stripTypeScriptTypes(code); console.log(strippedCode); // Prints: const a = 1;
const { stripTypeScriptTypes } = require('node:module'); const code = 'const a: number = 1;'; const strippedCode = stripTypeScriptTypes(code); console.log(strippedCode); // Prints: const a = 1;
If sourceUrl is provided, it will be used appended as a comment at the end of the output:
import { stripTypeScriptTypes } from 'node:module'; const code = 'const a: number = 1;'; const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' }); console.log(strippedCode); // Prints: const a = 1\n\n//# sourceURL=source.ts;
const { stripTypeScriptTypes } = require('node:module'); const code = 'const a: number = 1;'; const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' }); console.log(strippedCode); // Prints: const a = 1\n\n//# sourceURL=source.ts;
module.syncBuiltinESMExports(): void
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); });