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

module: add builtinModules #16386

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Module is exported as module.exports you can just bolt on builtinModules to

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) => { return mod.startsWith('internal/'); }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit: I’d just write (mod) => mod.startsWith('internal/')

[]
);