node/lib/internal
Joyee Cheung 1de215e285 process: add process.getBuiltinModule(id)
`process.getBuiltinModule(id)` provides a way to load 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 built-in
when it is run in Node.js, without having to deal with the resolution
error that can be thrown by `import` 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.getBuiltinModule) {
  // Run in Node.js, use the Node.js fs module.
  const fs = globalThis.process.getBuiltinModule('fs');
  // If `require()` is needed to load user-modules, use
  // createRequire()
  const module = globalThis.process.getBuiltinModule('module');
  const require = module.createRequire(import.meta.url);
  const foo = require('foo');
}
```

If `id` specifies a built-in module available in the current Node.js
process, `process.getBuiltinModule(id)` method returns the
corresponding built-in module. If `id` does not correspond to any
built-in module, `undefined` is returned.

`process.getBuiltinModule(id)` accept built-in module IDs that are
recognized by `module.isBuiltin(id)`. Some built-in modules must be
loaded with the `node:` prefix.

The built-in modules returned by `process.getBuiltinModule(id)` are
always the original modules - that is, it's not affected by
`require.cache`.

PR-URL: https://github.com/nodejs/node/pull/52762
Fixes: https://github.com/nodejs/node/issues/52599
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
2024-05-28 20:23:15 +00:00
..
assert lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
bootstrap process: add process.getBuiltinModule(id) 2024-05-28 20:23:15 +00:00
child_process tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
cluster cluster: replace forEach with for-of loop 2024-05-12 19:58:40 +02:00
console console: colorize console error and warn 2024-05-08 23:57:26 +10:00
crypto crypto: fix duplicated switch-case return values 2024-05-12 21:17:14 +02:00
debugger debugger: use internal/url.URL instead of url.parse 2023-09-13 18:03:14 +00:00
dns lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
events events: extract addAbortListener for safe internal use 2024-03-16 09:08:55 +00:00
fs fs: keep fs.promises.readFile read until EOF is reached 2024-05-11 15:39:21 +00:00
http2 http2: fix excessive CPU usage when using allowHTTP1=true 2024-04-28 19:18:07 +00:00
legacy tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
main bootstrap: print --help message using console.log 2024-05-12 21:43:13 +02:00
modules process: add process.getBuiltinModule(id) 2024-05-28 20:23:15 +00:00
per_context tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
perf perf_hooks: add deliveryType and responseStatus fields 2024-05-12 20:02:48 +02:00
process module: do not set CJS variables for Worker eval 2024-05-25 02:51:47 +00:00
readline lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
repl repl: fix disruptive autocomplete without inspector 2024-05-12 11:15:25 +02:00
source_map tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
streams lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
test v8: implement v8.queryObjects() for memory leak regression testing 2024-03-02 22:11:30 +00:00
test_runner test_runner,doc: align documentation with actual stdout/stderr behavior 2024-05-28 19:44:16 +00:00
tls tls: fix negative sessionTimeout handling 2024-05-18 00:26:11 +00:00
util assert: add deep equal check for more Error type 2024-05-12 19:46:13 +00:00
v8 lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
vm vm: fix ASCII-betical order 2024-04-25 19:14:16 +00:00
watch_mode watch: fix variable naming 2024-05-24 05:31:05 +00:00
webstreams stream: use ByteLengthQueuingStrategy when not in objectMode 2024-05-12 19:24:43 +02:00
worker lib: replace MessageEvent with undici's 2024-04-29 11:05:57 +00:00
abort_controller.js lib: use predefined variable instead of bit operation 2024-04-20 16:24:25 +00:00
assert.js
async_hooks.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
blob.js buffer: remove lines setting indexes to integer value 2024-05-06 12:45:31 +00:00
blocklist.js src: distinguish HTML transferable and cloneable 2023-07-07 17:00:00 +00:00
buffer.js src: throw DataCloneError on transfering untransferable objects 2023-05-05 11:22:42 +00:00
child_process.js errors: improve hideStackFrames 2023-11-11 16:25:08 +00:00
cli_table.js lib: align console.table row to the left 2023-10-28 01:14:22 +00:00
constants.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
dgram.js lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
encoding.js stream: implement min option for ReadableStreamBYOBReader.read 2024-01-04 12:57:14 +01:00
error_serdes.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
errors.js lib,src: remove --experimental-policy 2024-05-07 16:25:45 +00:00
event_target.js events: update MaxListenersExceededWarning message log 2024-05-12 18:16:09 +00:00
file.js lib,src: replace toUSVString with toWellFormed() 2023-11-29 16:00:57 +00:00
fixed_queue.js
freelist.js
freeze_intrinsics.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
heap_utils.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
histogram.js lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
http.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
idna.js src: replace idna functions with ada::idna 2023-04-28 18:48:38 +00:00
inspector_async_hook.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
js_stream_socket.js http2: fix h2-over-h2 connection proxying 2024-04-08 09:53:18 +00:00
linkedlist.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
mime.js util: pass invalidSubtypeIndex instead of trimmedSubtype to error 2023-12-25 04:29:00 +00:00
navigator.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
net.js lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
options.js src: reduce unnecessary serialization of CLI options in C++ 2024-05-02 22:57:03 +00:00
priority_queue.js test_runner: add Date to the supported mock APIs 2023-10-23 11:23:12 +00:00
promise_hooks.js tools: add lint rule to keep primordials in ASCII order 2024-04-21 16:53:08 +00:00
querystring.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
README.md lib, doc: rename readme.md to README.md 2024-04-14 10:30:31 +00:00
repl.js repl: add trailing commas in source files 2023-02-24 09:43:27 +01:00
socket_list.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
socketaddress.js src: distinguish HTML transferable and cloneable 2023-07-07 17:00:00 +00:00
stream_base_commons.js http2: fix h2-over-h2 connection proxying 2024-04-08 09:53:18 +00:00
timers.js timers: use V8 fast API calls 2023-02-28 09:37:24 +00:00
trace_events_async_hooks.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
tty.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
url.js lib, url: add a windows option to path parsing 2024-04-24 15:47:48 +00:00
util.js tools: update ESLint to v9 and use flat config 2024-05-23 19:45:18 +00:00
v8_prof_polyfill.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
v8_prof_processor.js src: implement natives binding without special casing 2023-06-09 15:32:55 +02:00
validators.js lib: enforce ASCII order in error code imports 2024-04-23 17:05:38 +00:00
vm.js typings: lib/internal/vm.js 2024-02-19 01:19:44 +00:00
wasm_web_api.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
watchdog.js lib: add trailing commas to more internal files 2023-02-26 11:34:02 +01:00
webidl.js stream: treat compression web stream format per its WebIDL definition 2023-11-12 10:01:51 +00:00
worker.js module: have a single hooks thread for all workers 2024-05-08 02:20:48 +00:00

Internal Modules

The modules in lib/internal are intended for internal use in Node.js core only, and are not accessible with require() from user modules. These modules can be changed at any time. Reliance on these modules outside of core is not supported in any way.