@flex-development/builtin-modules
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

builtin-modules

github release npm codecov module type: esm license conventional commits typescript vitest yarn

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

Contribute

See CONTRIBUTING.md.

Package Sidebar

Install

npm i @flex-development/builtin-modules

Weekly Downloads

3

Version

2.1.1

License

BSD-3-Clause

Unpacked Size

52.4 kB

Total Files

12

Last publish

Collaborators

  • unicornware