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 isBuiltin method #43396

Merged
merged 24 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ const require = createRequire(import.meta.url);
const siblingModule = require('./sibling-module');
```

### `module.isBuiltIn(moduleName)`
hemanth marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

* `moduleName` {string} name of the module
* Returns: {boolean} returns true if the module is builtin else returns false

```mjs
import { isBuiltIn } from 'node:module';
hemanth marked this conversation as resolved.
Show resolved Hide resolved
isBuiltIn('node:fs'); // true
hemanth marked this conversation as resolved.
Show resolved Hide resolved
isBuiltIn('fs'); // true
isBuiltIn('wss'); // false
```

### `module.syncBuiltinESMExports()`

<!-- YAML
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const {
StringPrototypeIndexOf,
StringPrototypeMatch,
StringPrototypeRepeat,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -1293,5 +1294,10 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
}
};

Module.isBuiltIn = function isBuiltIn(moduleName) {
hemanth marked this conversation as resolved.
Show resolved Hide resolved
moduleName = StringPrototypeReplace(String(moduleName), /^node:/, '');
return ArrayPrototypeIncludes(Module.builtinModules, moduleName);
hemanth marked this conversation as resolved.
Show resolved Hide resolved
};

// Backwards compatibility
Module.Module = Module;
13 changes: 13 additions & 0 deletions test/parallel/test-module-isBuiltIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
require('../common');
const assert = require('assert');
const { isBuiltIn } = require('module');

// Includes modules in lib/ (even deprecated ones)
assert(isBuiltIn('http'));
assert(isBuiltIn('sys'));
hemanth marked this conversation as resolved.
Show resolved Hide resolved

hemanth marked this conversation as resolved.
Show resolved Hide resolved
// Does not include internal modules
assert(!isBuiltIn('internal'));
hemanth marked this conversation as resolved.
Show resolved Hide resolved
hemanth marked this conversation as resolved.
Show resolved Hide resolved
assert(!isBuiltIn(''));
assert(!isBuiltIn(undefined));