builtin-modules
Universal drop-in replacement for module.builtinModules
Contents
What is this?
This package is a universal drop-in replacement for the builtinModules
constant exported by node:module
.
When should I use this?
This package exports an array containing the names of modules provided by Node.js. It can be used to not only verify
that a module is maintained by Node.js, but to also determine if a module can be imported using a node:
URL.
Install
This package is ESM only.
yarn add @flex-development/builtin-modules
From Git:
yarn add @flex-development/builtin-modules@flex-development/builtin-modules
See Git - Protocols | Yarn for details on requesting a specific branch, commit, or tag.
Use
import { builtinModules } from '@flex-development/builtin-modules'
/**
* Set containing the names of modules provided by Node.js.
*
* **Note**: Includes [`node:` URLs][1].
*
* [1]: https://nodejs.org/api/esm.html#node-imports
*
* @const {Set<string>} BUILTIN_MODULES
*/
const BUILTIN_MODULES = new Set(builtinModules.flatMap(m => [m, 'node:' + m]))
/**
* Checks if the given module `name` is the name of a [builtin module][1].
*
* Allows for [`node:` URLs][2].
*
* [1]: https://nodejs.org/api/esm.html#builtin-modules
* [2]: https://nodejs.org/api/esm.html#node-imports
*
* @example
* isBuiltin('node:module') // true
* @example
* isBuiltin('fs/promises') // true
* @example
* isBuiltin('@flex-development/builtin-modules') // false
*
* @param {string} name - Module name to evaluate
* @return {boolean} `true` if `name` is name of builtin module
*/
const isBuiltin = (name: string): boolean => BUILTIN_MODULES.has(name)
console.debug(isBuiltin('node:module'))
console.debug(isBuiltin('fs/promises'))
console.debug(isBuiltin('@flex-development/builtin-modules'))
API
This package exports the identifier builtinModules
.
There is no default export.
builtinModules
An array containing the names of modules provided by Node.js.
The array is a superset of module.builtinModules
given the running version of Node.js.
Possible use cases:
- Check if a module can be imported using a
node:
URL - Check if a module is maintained by Node.js
Types
This package is fully typed with TypeScript.
Related
-
is-builtin
— Universal drop-in replacement formodule.isBuiltin
Contribute
See CONTRIBUTING.md
.