Node.js Reference ยท process
Node.js process.getBuiltinModule() method
Reference for process.getBuiltinModule() in Node.js: load a core module synchronously without import or require, with feature detection and the node: prefix rules.
process.getBuiltinModule(id) returns the exports of a built-in (core) Node.js module such as node:fs or node:path, or undefined if there is no such module. It works the same in CommonJS files, in ES modules and in scripts where neither require nor a static import is convenient, and it is synchronous.
Its main use is portable code: a library can ask "is this a Node.js-like runtime with node:fs?" and continue gracefully if the answer is no, instead of failing at import time. Because it is a method on process, it also works in contexts where require is missing, for example inside a function evaluated from a string or in a bundle that is also run in another runtime.
const fs = process.getBuiltinModule('node:fs'); console.log(typeof fs.readFileSync);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.getBuiltinModule(id: string): object | undefined
| Name | Description |
|---|---|
id |
The module name. Both 'node:fs' and 'fs' work for ordinary core modules. |
Returns: the module's exports object, or undefined when id does not name a built-in module. It never loads files from disk or from node_modules.
Errors: a non-string id throws ERR_INVALID_ARG_TYPE.
Usage notes
- The
node:prefix is required for modules that are only available with it, such asnode:test,node:sqliteandnode:sea. Their bare names returnundefined. - Same exports as
require(). In CommonJS the result is identical torequire('node:fs'). In ES modules it is the CommonJS-style exports object, the same one that is the default export of a staticimport fs from 'node:fs'. - Never returns an npm package, even if one has the same name as a core module. This makes it safe for security-sensitive lookups.
- Synchronous. Unlike
await import('node:fs'), it needs noawaitand works inside non-async functions and at the top of a script. - Feature detection pattern:
process.getBuiltinModule?.('node:fs')returnsundefinedon runtimes or older Node.js versions that lack the method, so combine it with optional chaining. - Bundlers cannot see it. A static
importlets a bundler tree-shake and rewrite; calls togetBuiltinModuleare opaque and are kept as written. - Added in Node.js 22.3 and 20.16. On older versions use
requireormodule.createRequire(); seemodule.createRequire(). - Check the list of core modules with
module.builtinModules.