Skip to content

Commit

Permalink
module: add builtinModules
Browse files Browse the repository at this point in the history
Provides list of all builtin modules in Node.

Includes modules of all types:
- prefixed (ex: _tls_common)
- deprecated (ex: sys)
- regular (ex: vm)

Backport-PR-URL: #18221
PR-URL: #16386
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]>
  • Loading branch information
maclover7 authored and MylesBorins committed Feb 13, 2018
1 parent 2268d00 commit cdf4a9c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,5 +633,29 @@ object. Since `require()` returns the `module.exports`, and the `module` is
typically *only* available within a specific module's code, it must be
explicitly exported in order to be used.

## The `Module` Object

<!-- YAML
added: v0.3.7
-->

* {Object}

Provides general utility methods when interacting with instances of
`Module` -- the `module` variable often seen in file modules. Accessed
via `require('module')`.

### module.builtinModules
<!-- YAML
added: REPLACEME
-->

* {string[]}

A list of the names of all modules provided by Node.js. Can be used to verify
if a module is maintained by a third-party module or not.

[`__dirname`]: #modules_dirname
[`__filename`]: #modules_filename
[`Error`]: errors.html#errors_class_error
[module resolution]: #modules_all_together
7 changes: 7 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ function Module(id, parent) {
}
module.exports = Module;

const builtinModules = Object.keys(NativeModule._source)
.filter(NativeModule.nonInternalExists);

Object.freeze(builtinModules);
Module.builtinModules = builtinModules;

Module._cache = {};
Module._pathCache = {};
Module._extensions = {};

var modulePaths = [];
Module.globalPaths = [];

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-module-builtin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
require('../common');
const assert = require('assert');
const { builtinModules } = require('module');

// Includes modules in lib/ (even deprecated ones)
assert(builtinModules.includes('http'));
assert(builtinModules.includes('sys'));

// Does not include internal modules
assert.deepStrictEqual(
builtinModules.filter((mod) => mod.startsWith('internal/')),
[]
);

0 comments on commit cdf4a9c

Please sign in to comment.