Skip to content

Commit

Permalink
module: add isBuiltIn method
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth authored Jun 14, 2022
1 parent 5ca2ab1 commit a66a302
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ for (const { 0: id, 1: mod } of NativeModule.map) {
ObjectFreeze(builtinModules);
Module.builtinModules = builtinModules;


Module._cache = ObjectCreate(null);
Module._pathCache = ObjectCreate(null);
Module._extensions = ObjectCreate(null);
Expand Down Expand Up @@ -1293,5 +1294,10 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
}
};

Module.isBuiltIn = function isBuiltIn(moduleName) {
moduleName = String(moduleName).replace('node:', '');
return Module.builtinModules.includes(moduleName);
};

// Backwards compatibility
Module.Module = Module;
16 changes: 16 additions & 0 deletions test/parallel/test-module-isBuiltIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'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'));

// Does not include internal modules
assert(!isBuiltIn('internal'));
assert(!isBuiltIn(''));
assert(!isBuiltIn(undefined));



0 comments on commit a66a302

Please sign in to comment.