mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
1de215e285
`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> |
||
---|---|---|
.. | ||
assert | ||
bootstrap | ||
child_process | ||
cluster | ||
console | ||
crypto | ||
debugger | ||
dns | ||
events | ||
fs | ||
http2 | ||
legacy | ||
main | ||
modules | ||
per_context | ||
perf | ||
process | ||
readline | ||
repl | ||
source_map | ||
streams | ||
test | ||
test_runner | ||
tls | ||
util | ||
v8 | ||
vm | ||
watch_mode | ||
webstreams | ||
worker | ||
abort_controller.js | ||
assert.js | ||
async_hooks.js | ||
blob.js | ||
blocklist.js | ||
buffer.js | ||
child_process.js | ||
cli_table.js | ||
constants.js | ||
dgram.js | ||
encoding.js | ||
error_serdes.js | ||
errors.js | ||
event_target.js | ||
file.js | ||
fixed_queue.js | ||
freelist.js | ||
freeze_intrinsics.js | ||
heap_utils.js | ||
histogram.js | ||
http.js | ||
idna.js | ||
inspector_async_hook.js | ||
js_stream_socket.js | ||
linkedlist.js | ||
mime.js | ||
navigator.js | ||
net.js | ||
options.js | ||
priority_queue.js | ||
promise_hooks.js | ||
querystring.js | ||
README.md | ||
repl.js | ||
socket_list.js | ||
socketaddress.js | ||
stream_base_commons.js | ||
timers.js | ||
trace_events_async_hooks.js | ||
tty.js | ||
url.js | ||
util.js | ||
v8_prof_polyfill.js | ||
v8_prof_processor.js | ||
validators.js | ||
vm.js | ||
wasm_web_api.js | ||
watchdog.js | ||
webidl.js | ||
worker.js |
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.