Skip to content

module: add API for identifying core modules #16555

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

Closed
wants to merge 1 commit into from
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
26 changes: 26 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,32 @@ 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.

## Listing and Identifying Core Modules

The current set of the core modules may be identified using the
`Module.isCoreModule()` and `Module.listCoreModules()` methods.

```js
const {
isCoreModule,
listCoreModules
} = require('module');

console.log(isCoreModule('http2')); // true
console.log(isCoreModule('foo')); // false

listCoreModules().forEach(console.log);
```

### Module.isCoreModule(id)

* `id` {string} A module name
* Returns `true` if `id` identifies a core module, false otherwise

### Module.getCoreModules()

* Returns: An array of core module names

[`__dirname`]: #modules_dirname
[`__filename`]: #modules_filename
[`Error`]: errors.html#errors_class_error
Expand Down
12 changes: 12 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const {
const preserveSymlinks = !!process.binding('config').preserveSymlinks;
const experimentalModules = !!process.binding('config').experimentalModules;

const kCoreModules =
new Set(Object.keys(process.binding('natives'))
.filter((name) => !name.startsWith('internal/')));

const errors = require('internal/errors');

const Loader = require('internal/loader/Loader');
Expand Down Expand Up @@ -74,6 +78,14 @@ function Module(id, parent) {
}
module.exports = Module;

Module.getCoreModules = function getCoreModules() {
return Array.from(kCoreModules);
};

Module.isCoreModule = function isCoreModule(name) {
return kCoreModules.has(name);
};

Module._cache = Object.create(null);
Module._pathCache = Object.create(null);
Module._extensions = Object.create(null);
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-module-core-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

require('../common');
const assert = require('assert');
const natives = process.binding('natives');
const {
isCoreModule,
getCoreModules
} = require('module');

const keys = Object.keys(natives);

keys.filter((name) => !name.startsWith('internal/'))
.forEach((i) => assert(isCoreModule(i)));

keys.filter((name) => name.startsWith('internal/'))
.forEach((i) => assert(!isCoreModule(i)));

['foo', 'bar', 'baz']
.forEach((i) => assert(!isCoreModule(i)));

const list = getCoreModules();

assert(Array.isArray(list));
assert.deepStrictEqual(list,
keys.filter((name) => !name.startsWith('internal/')));