Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,30 @@ process.kill(process.pid, 'SIGHUP');
When `SIGUSR1` is received by a Node.js process, Node.js will start the
debugger. See [Signal Events][].

## `process.loadedModules`

<!-- YAML
added: REPLACEME
-->

* Type: {string\[]}
The `process.loadedModules` property returns an array of core modules that
were loaded during the current Node.js process execution.

```mjs
import process from 'node:process';

console.log(process.loadedModules);
// ['events', 'buffer', 'diagnostics_channel', 'async_hooks', ...]
```

```cjs
const process = require('node:process');

console.log(process.loadedModules);
// ['events', 'buffer', 'diagnostics_channel', 'async_hooks', ...]
```

## `process.loadEnvFile(path)`

<!-- YAML
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/bootstrap/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ ObjectDefineProperty(process, 'moduleLoadList', {
writable: false,
});

// Set up process.loadedModules
const loadedModules = [];
ObjectDefineProperty(process, 'loadedModules', {
__proto__: null,
get() { return ArrayPrototypeSlice(loadedModules); },
configurable: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make it configurable?

enumerable: true,
});


// processBindingAllowList contains the name of bindings that are allowed
// for access via process.binding(). This is used to provide a transition
Expand Down Expand Up @@ -405,6 +414,9 @@ class BuiltinModule {
// "NativeModule" is a legacy name of "BuiltinModule". We keep it
// here to avoid breaking users who parse process.moduleLoadList.
ArrayPrototypePush(moduleLoadList, `NativeModule ${id}`);
if (!StringPrototypeStartsWith(id, 'internal/')) {
ArrayPrototypePush(loadedModules, id);
}
Comment on lines +417 to +419
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this handle #61276 (comment) and #61276 (comment) from Joyee had on your previous doc PR?

I think it specifically doesn't handle:

the names of the modules are also highly volatile and cannot be reliably depended on

return this.exports;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-process-loadedmodules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../common');
const assert = require('assert');

assert.ok(Array.isArray(process.loadedModules));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a little more on what's in that is actually what it should be :)

assert.throws(() => process.loadedModules = 'foo', /^TypeError: Cannot set property loadedModules of #<process> which has only a getter$/);
Loading