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)

PR-URL: #16386
Refs: #3307
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
maclover7 authored and MylesBorins committed Dec 12, 2017
1 parent 94be7fd commit 7ae7124
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,28 @@ The `module.require` method provides a way to load a module as if
`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
Expand Down
6 changes: 6 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ function Module(id, parent) {
}
module.exports = Module;

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

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

Module._cache = Object.create(null);
Module._pathCache = Object.create(null);
Module._extensions = Object.create(null);
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 7ae7124

Please sign in to comment.