Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v6.x] module: add builtinModules #18221

Closed
Closed
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/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/')),
[]
);