Node.js v26.1.0 documentation
- Node.js v26.1.0
- Table of contents
- Modules:
node:moduleAPI- The
Moduleobject - Module compile cache
- Customization Hooks
- Synchronous customization hooks
- Asynchronous customization hooks
- Caveats of asynchronous customization hooks
- Registration of asynchronous customization hooks
- Chaining of asynchronous customization hooks
- Communication with asynchronous module customization hooks
- Asynchronous hooks accepted by
module.register() initialize()- Asynchronous
resolve(specifier, context, nextResolve) - Asynchronous
load(url, context, nextLoad)
- Examples
- Source Map Support
- The
- Modules:
- Index
- 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
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- FFI
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Iterable Streams API
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- 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
- Other versions
- Options
Modules: node:module API#
The Module object#
- Type:
<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#
- Type:
<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)#
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.findPackageJSON(specifier[, base])#
Stability: 1.1 - Active Development
specifier<string>|<URL>The specifier for the module whosepackage.jsonto retrieve. When passing a bare specifier, thepackage.jsonat the root of the package is returned. When passing a relative specifier or an absolute specifier, the closest parentpackage.jsonis returned.base<string>|<URL>The absolute location (file:URL string or FS path) of the containing module. For CJS, use__filename(not__dirname!); for ESM, useimport.meta.url. You do not need to pass it ifspecifieris anabsolute specifier.- Returns:
<string>|<undefined>A path if thepackage.jsonis found. Whenspecifieris a package, the package's rootpackage.json; when a relative or unresolved, the closestpackage.jsonto thespecifier.
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
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(