Skip to content

Commit fa9a95c

Browse files
committed
lib: add process.loadedModules list
Add a new `process.loadedModules` property that returns an array of public core module names that have been loaded during the current Node.js process execution. This provides a cleaner, documented API for accessing loaded module information compared to the undocumented `process.moduleLoadList`. Fixes: nodejs#41233 Refs: nodejs#61276
1 parent 61d0a95 commit fa9a95c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

doc/api/process.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,30 @@ process.kill(process.pid, 'SIGHUP');
26962696
When `SIGUSR1` is received by a Node.js process, Node.js will start the
26972697
debugger. See [Signal Events][].
26982698
2699+
## `process.loadedModules`
2700+
2701+
<!-- YAML
2702+
added: REPLACEME
2703+
-->
2704+
2705+
* Type: {string\[]}
2706+
The `process.loadedModules` property returns an array of core modules that
2707+
were loaded during the current Node.js process execution.
2708+
2709+
```mjs
2710+
import process from 'node:process';
2711+
2712+
console.log(process.loadedModules);
2713+
// ['events', 'buffer', 'diagnostics_channel', 'async_hooks', ...]
2714+
```
2715+
2716+
```cjs
2717+
const process = require('node:process');
2718+
2719+
console.log(process.loadedModules);
2720+
// ['events', 'buffer', 'diagnostics_channel', 'async_hooks', ...]
2721+
```
2722+
26992723
## `process.loadEnvFile(path)`
27002724
27012725
<!-- YAML

lib/internal/bootstrap/realm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ ObjectDefineProperty(process, 'moduleLoadList', {
8080
writable: false,
8181
});
8282

83+
// Set up process.loadedModules
84+
const loadedModules = [];
85+
ObjectDefineProperty(process, 'loadedModules', {
86+
__proto__: null,
87+
get() { return ArrayPrototypeSlice(loadedModules); },
88+
configurable: true,
89+
enumerable: true,
90+
});
91+
8392

8493
// processBindingAllowList contains the name of bindings that are allowed
8594
// for access via process.binding(). This is used to provide a transition
@@ -405,6 +414,9 @@ class BuiltinModule {
405414
// "NativeModule" is a legacy name of "BuiltinModule". We keep it
406415
// here to avoid breaking users who parse process.moduleLoadList.
407416
ArrayPrototypePush(moduleLoadList, `NativeModule ${id}`);
417+
if (!StringPrototypeStartsWith(id, 'internal/')) {
418+
ArrayPrototypePush(loadedModules, id);
419+
}
408420
return this.exports;
409421
}
410422
}

0 commit comments

Comments
 (0)