| 1 |
# is-core-module <sup>[![Version Badge][2]][1]</sup> |
| 2 |
|
| 3 |
[![github actions][actions-image]][actions-url] |
| 4 |
[![coverage][codecov-image]][codecov-url] |
| 5 |
[![dependency status][5]][6] |
| 6 |
[![dev dependency status][7]][8] |
| 7 |
[![License][license-image]][license-url] |
| 8 |
[![Downloads][downloads-image]][downloads-url] |
| 9 |
|
| 10 |
[![npm badge][11]][1] |
| 11 |
|
| 12 |
Is this specifier a node.js core module? Optionally provide a node version to check; defaults to the current node version. |
| 13 |
|
| 14 |
## Example |
| 15 |
|
| 16 |
```js |
| 17 |
var isCore = require('is-core-module'); |
| 18 |
var assert = require('assert'); |
| 19 |
assert(isCore('fs')); |
| 20 |
assert(!isCore('butts')); |
| 21 |
``` |
| 22 |
|
| 23 |
## TypeScript |
| 24 |
|
| 25 |
`isCore` accepts any string. `isCore.Module` is the union of every specifier in `core.json`, regardless of node version, so editors can suggest them. |
| 26 |
|
| 27 |
The types import `core.json`, so [`resolveJsonModule`](https://www.typescriptlang.org/tsconfig/#resolveJsonModule), [`esModuleInterop`](https://www.typescriptlang.org/tsconfig/#esModuleInterop), and [`allowSyntheticDefaultImports`](https://www.typescriptlang.org/tsconfig/#allowSyntheticDefaultImports) must be enabled; some `module` and `moduleResolution` settings enable them by default, but `node16` does not enable `resolveJsonModule`. |
| 28 |
Without them, TypeScript reports an error in this package's `index.d.ts`, or, with `skipLibCheck`, `isCore.Module` silently widens to `string | number | symbol`. |
| 29 |
|
| 30 |
```ts |
| 31 |
import isCore = require('is-core-module'); |
| 32 |
|
| 33 |
function isBuiltin(specifier: string): boolean { |
| 34 |
return isCore(specifier); // any string is accepted |
| 35 |
} |
| 36 |
|
| 37 |
const name: isCore.Module = 'node:fs'; // only specifiers listed in `core.json` |
| 38 |
isCore(name, '16.0.0'); // true |
| 39 |
``` |
| 40 |
|
| 41 |
## Tests |
| 42 |
Clone the repo, `npm install`, and run `npm test` |
| 43 |
|
| 44 |
[1]: https://npmjs.org/package/is-core-module |
| 45 |
[2]: https://versionbadg.es/inspect-js/is-core-module.svg |
| 46 |
[5]: https://david-dm.org/inspect-js/is-core-module.svg |
| 47 |
[6]: https://david-dm.org/inspect-js/is-core-module |
| 48 |
[7]: https://david-dm.org/inspect-js/is-core-module/dev-status.svg |
| 49 |
[8]: https://david-dm.org/inspect-js/is-core-module#info=devDependencies |
| 50 |
[11]: https://nodei.co/npm/is-core-module.png?downloads=true&stars=true |
| 51 |
[license-image]: https://img.shields.io/npm/l/is-core-module.svg |
| 52 |
[license-url]: LICENSE |
| 53 |
[downloads-image]: https://img.shields.io/npm/dm/is-core-module.svg |
| 54 |
[downloads-url]: https://npm-stat.com/charts.html?package=is-core-module |
| 55 |
[codecov-image]: https://codecov.io/gh/inspect-js/is-core-module/branch/main/graphs/badge.svg |
| 56 |
[codecov-url]: https://app.codecov.io/gh/inspect-js/is-core-module/ |
| 57 |
[actions-image]: https://img.shields.io/github/check-runs/inspect-js/is-core-module/main |
| 58 |
[actions-url]: https://github.com/inspect-js/is-core-module/actions |