Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 30, 2024
1 parent 2b22980 commit 52df82e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 57 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

10 changes: 3 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +19,4 @@ isBuiltinModule('unicorn');
//=> false
```
*/
declare function isBuiltinModule(moduleName: string): boolean;

export = isBuiltinModule;
export default function isBuiltinModule(moduleName: string): boolean;
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
}
Expand All @@ -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);
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
24 changes: 5 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,20 +28,6 @@ isBuiltinModule('unicorn');
//=> false
```


## Related

- [builtin-modules](https://github.com/sindresorhus/builtin-modules) - List of the Node.js builtin modules


---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-is-builtin-module?utm_source=npm-is-builtin-module&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
- [builtin-modules](https://github.com/sindresorhus/builtin-modules) - A static list of the Node.js builtin modules from the latest Node.js version
9 changes: 4 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import isBuiltinModule from '.';
import isBuiltinModule from './index.js';

test('main', t => {
t.true(isBuiltinModule('fs'));
Expand All @@ -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'));
Expand Down

0 comments on commit 52df82e

Please sign in to comment.