Skip to content

Commit 011fe1d

Browse files
BridgeARcodebytere
authored andcommitted
repl: add builtinModules
This adds an alias to `_builtinLibs` that is documented and should as such also be accessed publicly. It does not contain any underscored modules. Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33295 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 500bad1 commit 011fe1d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

doc/api/repl.md

+9
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ by default. However, this is not the case when creating a REPL
540540
programmatically. Use this method to initialize a history log file when working
541541
with REPL instances programmatically.
542542

543+
## `repl.builtinModules`
544+
<!-- YAML
545+
added: REPLACEME
546+
-->
547+
548+
* {string[]}
549+
550+
A list of the names of all Node.js modules, e.g., `'http'`.
551+
543552
## `repl.start([options])`
544553
<!-- YAML
545554
added: v0.1.91

lib/repl.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const {
8686
const { Console } = require('console');
8787
const CJSModule = require('internal/modules/cjs/loader').Module;
8888
let _builtinLibs = [...CJSModule.builtinModules]
89-
.filter((e) => !e.startsWith('_'));
89+
.filter((e) => !e.startsWith('_') && !e.includes('/'));
9090
const domain = require('domain');
9191
const debug = require('internal/util/debuglog').debuglog('repl');
9292
const {
@@ -1593,6 +1593,13 @@ module.exports = {
15931593
Recoverable
15941594
};
15951595

1596+
ObjectDefineProperty(module.exports, 'builtinModules', {
1597+
get: () => _builtinLibs,
1598+
set: (val) => _builtinLibs = val,
1599+
enumerable: true,
1600+
configurable: true
1601+
});
1602+
15961603
ObjectDefineProperty(module.exports, '_builtinLibs', {
15971604
get: pendingDeprecation ? deprecate(
15981605
() => _builtinLibs,

test/parallel/test-repl-tab-complete.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,18 @@ putIn.run(['.clear']);
237237
testMe.complete('require(\'', common.mustCall(function(error, data) {
238238
assert.strictEqual(error, null);
239239
builtinModules.forEach((lib) => {
240-
if (!lib.startsWith('_'))
241-
assert(data[0].includes(lib), `${lib} not found`);
240+
assert(
241+
data[0].includes(lib) || lib.startsWith('_') || lib.includes('/'),
242+
`${lib} not found`
243+
);
242244
});
245+
const newModule = 'foobar';
246+
assert(!builtinModules.includes(newModule));
247+
repl.builtinModules.push(newModule);
248+
testMe.complete('require(\'', common.mustCall((_, [modules]) => {
249+
assert.strictEqual(data[0].length + 1, modules.length);
250+
assert(modules.includes(newModule));
251+
}));
243252
}));
244253

245254
testMe.complete("require\t( 'n", common.mustCall(function(error, data) {

0 commit comments

Comments
 (0)