Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
module: add isBuiltIn method
  • Loading branch information
hemanth authored Jun 14, 2022
commit ec1f95aa30323f0c5b232ebca799d42db9506bd4
5 changes: 5 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,5 +1293,10 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
}
};

Module.isBuiltIn = function isBuiltIn(moduleName) {
Comment thread
hemanth marked this conversation as resolved.
Outdated
moduleName = String(moduleName).replace('node:', '');
Comment thread
hemanth marked this conversation as resolved.
Outdated
return Module.builtinModules.includes(moduleName);
Comment thread
hemanth marked this conversation as resolved.
Outdated
};

// 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'));
Comment thread
hemanth marked this conversation as resolved.
Outdated

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