Commit 719213c
committed
process: add process.getBuiltin(id)
`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.1 parent d20515a commit 719213c
File tree
4 files changed
+90
-0
lines changed- doc/api
- lib/internal
- bootstrap
- modules
- test/parallel
4 files changed
+90
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1917 | 1917 | | |
1918 | 1918 | | |
1919 | 1919 | | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
1920 | 1958 | | |
1921 | 1959 | | |
1922 | 1960 | | |
| |||
4010 | 4048 | | |
4011 | 4049 | | |
4012 | 4050 | | |
| 4051 | + | |
4013 | 4052 | | |
4014 | 4053 | | |
4015 | 4054 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
355 | 360 | | |
356 | 361 | | |
357 | 362 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
345 | 358 | | |
346 | 359 | | |
| 360 | + | |
347 | 361 | | |
348 | 362 | | |
349 | 363 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
0 commit comments