diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index cee682d..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,4 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -tidelift: npm/is-builtin-module -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 320c7b1..346585c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,15 +10,11 @@ jobs: fail-fast: false matrix: node-version: + - 20 - 18 - - 16 - - 14 - - 12 - - 10 - - 8 steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 00b79fe..30e4e6d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,11 +1,13 @@ /** -Returns `true` if the given `moduleName` is a Node.js builtin module, `false` otherwise. +Check if a string matches the name of a Node.js builtin module. -@param moduleName - The name of the module. +This matches based a [static list of modules](https://github.com/sindresorhus/builtin-modules) from the latest Node.js version. If you want to check for a module in the current Node.js, use the core [`isBuiltin`](https://nodejs.org/api/module.html#moduleisbuiltinmodulename) method. + +@param moduleName - The name of the module. @example ``` -import isBuiltinModule = require('is-builtin-module'); +import isBuiltinModule from 'is-builtin-module'; isBuiltinModule('fs/promises'); //=> true @@ -17,6 +19,4 @@ isBuiltinModule('unicorn'); //=> false ``` */ -declare function isBuiltinModule(moduleName: string): boolean; - -export = isBuiltinModule; +export default function isBuiltinModule(moduleName: string): boolean; diff --git a/index.js b/index.js index e79a925..9589873 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,9 @@ -'use strict'; -const builtinModules = require('builtin-modules'); +import builtinModules from 'builtin-modules'; const moduleSet = new Set(builtinModules); const NODE_PROTOCOL = 'node:'; -module.exports = moduleName => { +export default function isBuiltinModule(moduleName) { if (typeof moduleName !== 'string') { throw new TypeError('Expected a string'); } @@ -13,10 +12,5 @@ module.exports = moduleName => { moduleName = moduleName.slice(NODE_PROTOCOL.length); } - const slashIndex = moduleName.indexOf('/'); - if (slashIndex !== -1 && slashIndex !== moduleName.length - 1) { - moduleName = moduleName.slice(0, slashIndex); - } - return moduleSet.has(moduleName); -}; +} diff --git a/index.test-d.ts b/index.test-d.ts index 98d0b3e..3dc9439 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType, expectError} from 'tsd'; -import isBuiltinModule = require('.'); +import isBuiltinModule from './index.js'; expectType<(moduleName: string) => boolean>(isBuiltinModule); expectError<() => boolean>(isBuiltinModule); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 866e5ba..1fc7b1f 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,14 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "sideEffects": false, "engines": { - "node": ">=6" + "node": ">=18.20" }, "scripts": { "test": "xo && ava && tsd" @@ -37,11 +43,11 @@ "match" ], "dependencies": { - "builtin-modules": "^3.3.0" + "builtin-modules": "^4.0.0" }, "devDependencies": { - "ava": "^0.25.0", - "tsd": "^0.7.2", - "xo": "^0.23.0" + "ava": "^6.1.2", + "tsd": "^0.31.0", + "xo": "^0.58.0" } } diff --git a/readme.md b/readme.md index 8304ebd..b6ef78c 100644 --- a/readme.md +++ b/readme.md @@ -2,18 +2,18 @@ > Check if a string matches the name of a Node.js builtin module +Note that this matches based a [static list of modules](https://github.com/sindresorhus/builtin-modules) from the latest Node.js version. If you want to check for a module in the current Node.js, use the core [`isBuiltin`](https://nodejs.org/api/module.html#moduleisbuiltinmodulename) method. ## Install +```sh +npm install is-builtin-module ``` -$ npm install is-builtin-module -``` - ## Usage ```js -const isBuiltinModule = require('is-builtin-module'); +import isBuiltinModule from 'is-builtin-module'; isBuiltinModule('fs'); //=> true @@ -28,20 +28,6 @@ isBuiltinModule('unicorn'); //=> false ``` - ## Related -- [builtin-modules](https://github.com/sindresorhus/builtin-modules) - List of the Node.js builtin modules - - ---- - -
- - Get professional support for this package with a Tidelift subscription - -
- - Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. -
-
+- [builtin-modules](https://github.com/sindresorhus/builtin-modules) - A static list of the Node.js builtin modules from the latest Node.js version diff --git a/test.js b/test.js index f0f41c6..5fbab9e 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import isBuiltinModule from '.'; +import isBuiltinModule from './index.js'; test('main', t => { t.true(isBuiltinModule('fs')); @@ -9,10 +9,9 @@ test('main', t => { t.true(isBuiltinModule('fs/promises')); t.true(isBuiltinModule('assert/strict')); - // These are actually not, but should not exist - t.true(isBuiltinModule('fs/unknown')); - t.true(isBuiltinModule('fs/promises/unknown')); - t.true(isBuiltinModule('fs/promises?query=1')); + t.false(isBuiltinModule('fs/unknown')); + t.false(isBuiltinModule('fs/promises/unknown')); + t.false(isBuiltinModule('fs/promises?query=1')); t.true(isBuiltinModule('node:fs')); t.true(isBuiltinModule('node:fs/promises'));