Check if a string matches the name of a Node.js builtin module
npx @tessl/cli install tessl/npm-is-builtin-module@5.0.0is-builtin-module provides a simple utility function to check if a string matches the name of a Node.js builtin module. It uses a static list of modules from the latest Node.js version (via the builtin-modules package) rather than runtime checks, making it ideal for static analysis tools, bundlers, and linters.
npm install is-builtin-moduleES Modules:
import isBuiltinModule from 'is-builtin-module';CommonJS:
const isBuiltinModule = require('is-builtin-module');TypeScript:
import isBuiltinModule from 'is-builtin-module';import isBuiltinModule from 'is-builtin-module';
// Check basic builtin modules
isBuiltinModule('fs');
//=> true
isBuiltinModule('path');
//=> true
// Check submodules
isBuiltinModule('fs/promises');
//=> true
// Check with node: prefix
isBuiltinModule('node:fs/promises');
//=> true
// Check non-builtin modules
isBuiltinModule('express');
//=> false
isBuiltinModule('react');
//=> falseChecks if a string matches the name of a Node.js builtin module using a static list from the builtin-modules package.
/**
* Check if a string matches the name of a Node.js builtin module.
*
* This matches based on a static list of modules from the latest Node.js version.
* If you want to check for a module in the current Node.js, use the core
* isBuiltin method from the Node.js module API.
*
* @param moduleName - The name of the module to check
* @returns true if the module name is a builtin module, false otherwise
* @throws TypeError if moduleName is not a string
*/
export default function isBuiltinModule(moduleName: string): boolean;Parameter Details:
moduleName (string): The name of the module to check. Can include:
'fs', 'path', 'crypto')'fs/promises', 'assert/strict')'node:fs', 'node:fs/promises')Return Value:
true if the module name matches a Node.js builtin modulefalse if the module name is not a builtin moduleError Handling:
TypeError with message "Expected a string" if moduleName is not a stringImplementation Notes:
builtin-modules package, not runtime Node.js detection'FS' returns false)Supported Module Patterns:
'fs', 'path', 'crypto', 'http', etc.'fs/promises', 'assert/strict', 'stream/promises', etc.'node:fs', 'node:fs/promises', 'node:test', etc.'node:test' (only works with node: prefix)Edge Cases:
'punycode' returns false'fs/' return false'nodE:fs' or 'node:fS' return false'node:node:fs' or 'node:/fs' return false