-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`process.getBuiltin(id)` provides a way to load the built-in modules in a globally available function. ES Modules that need to support other environments can use it to conditionally load a Node.js builtin when it is run in Node.js, without having to deal with the resolution error that can be thrown by `import 'node:id'` in a non-Node.js environment or having to use dynamic `import()` which either turns the module into an asynchronous module, or turns a synchronous API into an asynchronous one. ```mjs if (globalThis.process && globalThis.process.getBuiltin) { // Run in Node.js, use the Node.js fs module. const fs = globalThis.process.getBuiltin('fs'); // If `require()` is needed to load user-modules, use createRequire() const m = globalThis.process.getBuiltin('module'); const require = m.createRequire(import.meta.url); const foo = require('foo'); } ``` If `id` specifies a built-in available in the current Node.js process, `process.getBuiltin()` method returns the corresponding built-in module, which is the same as the object returned by [`require(id)`][`require()`]. If `id` does not correspond to any built-in module, an [`ERR_UNKNOWN_BUILTIN_MODULE`][] would be thrown. Unlike [`require(id)`][`require()`], `process.getBuiltin(id)` does not need or accept IDs with the `node:` prefix.
- Loading branch information
1 parent
d20515a
commit 719213c
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import '../common/index.mjs'; | ||
import assert from 'node:assert'; | ||
import { builtinModules } from 'node:module'; | ||
|
||
const { getBuiltin } = globalThis.process; | ||
for (const invalid of [1, undefined, null, false, [], {}, () => {}, Symbol('test')]) { | ||
assert.throws(() => getBuiltin(invalid), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
} | ||
|
||
for (const invalid of [ | ||
'invalid', 'node:test', 'node:fs', 'internal/bootstrap/realm', | ||
'internal/deps/undici/undici', 'internal/util', | ||
]) { | ||
assert.throws(() => getBuiltin(invalid), { code: 'ERR_UNKNOWN_BUILTIN_MODULE' }); | ||
} | ||
|
||
// Check that createRequire()(id) returns the same thing as getBuiltin(id). | ||
const require = getBuiltin('module').createRequire(import.meta.url); | ||
for (const id of builtinModules) { | ||
assert.strictEqual(getBuiltin(id), require(id)); | ||
} | ||
// builtinModules does not include 'test' which requires the node: prefix. | ||
const ids = builtinModules.concat(['test']); | ||
for (const id of builtinModules) { | ||
assert.strictEqual(getBuiltin(id), require(`node:${id}`)); | ||
} | ||
|
||
// Check that import(id).default returns the same thing as getBuiltin(id). | ||
for (const id of ids) { | ||
const imported = await import(`node:${id}`); | ||
assert.strictEqual(getBuiltin(id), imported.default); | ||
} |