Skip to content

Commit c5093fc

Browse files
maclover7gibfahn
authored andcommitted
module: add builtinModules
Provides list of all builtin modules in Node. Includes modules of all types: - prefixed (ex: _tls_common) - deprecated (ex: sys) - regular (ex: vm) PR-URL: #16386 Backport-PR-URL: #18220 Refs: #3307 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 3588730 commit c5093fc

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

doc/api/modules.md

+22
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,28 @@ The `module.require` method provides a way to load a module as if
819819
`module` is typically *only* available within a specific module's code, it must
820820
be explicitly exported in order to be used.
821821

822+
## The `Module` Object
823+
824+
<!-- YAML
825+
added: v0.3.7
826+
-->
827+
828+
* {Object}
829+
830+
Provides general utility methods when interacting with instances of
831+
`Module` -- the `module` variable often seen in file modules. Accessed
832+
via `require('module')`.
833+
834+
### module.builtinModules
835+
<!-- YAML
836+
added: REPLACEME
837+
-->
838+
839+
* {string[]}
840+
841+
A list of the names of all modules provided by Node.js. Can be used to verify
842+
if a module is maintained by a third-party module or not.
843+
822844
[`__dirname`]: #modules_dirname
823845
[`__filename`]: #modules_filename
824846
[`Error`]: errors.html#errors_class_error

lib/module.js

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ function Module(id, parent) {
7676
this.children = [];
7777
}
7878

79+
const builtinModules = Object.keys(NativeModule._source)
80+
.filter(NativeModule.nonInternalExists);
81+
82+
Object.freeze(builtinModules);
83+
Module.builtinModules = builtinModules;
84+
7985
Module._cache = Object.create(null);
8086
Module._pathCache = Object.create(null);
8187
Module._extensions = Object.create(null);

test/parallel/test-module-builtin.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { builtinModules } = require('module');
5+
6+
// Includes modules in lib/ (even deprecated ones)
7+
assert(builtinModules.includes('http'));
8+
assert(builtinModules.includes('sys'));
9+
10+
// Does not include internal modules
11+
assert.deepStrictEqual(
12+
builtinModules.filter((mod) => mod.startsWith('internal/')),
13+
[]
14+
);

0 commit comments

Comments
 (0)