Skip to content

Commit 194eab9

Browse files
committed
Add a Readme file
1 parent fd2c950 commit 194eab9

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# eslint-plugin-node-imports
2+
3+
> An ESLint plugin that contains a rule to prevent imports from Node.js core modules
4+
5+
Have you ever accidentally made an import from a Node.js core module like `assert` or `fs` in an environment that does not support these core modules? `eslint-plugin-node-imports` helps: This plugin contains a rule that forbids imports from Node.js core modules.
6+
7+
## Usage
8+
9+
First, install this eslint plugin:
10+
11+
```
12+
npm install --save-dev eslint-plugin-node-imports
13+
```
14+
15+
Then, add the following to the `plugins` sections of your eslint config file:
16+
17+
```js
18+
plugins: ['node-imports'],
19+
```
20+
21+
Finally, you can activate the rule:
22+
23+
```js
24+
rules: {
25+
'node-imports/no-node-import': 'error',
26+
}
27+
```
28+
29+
## Advanced configuration
30+
31+
It is possible to allow certain modules. For instance, to allow the "fs" module, use the following setting:
32+
33+
```js
34+
rules: {
35+
'node-imports/no-node-import': ['error', { allowList: ['fs'] }],
36+
}
37+
```
38+
39+
**Important:** It is only possible to allow top-level modules. If, for instance, you want to allow imports from `assert/strict`, you have to allow the entire `assert` module:
40+
41+
```js
42+
rules: {
43+
'node-imports/no-node-import': ['error', { allowList: ['assert'] }],
44+
}
45+
```
46+
47+
## Examples
48+
49+
Examples of **incorrect** code:
50+
51+
```ts
52+
// 'node-imports/no-node-import': 'error',
53+
import { chmod } from 'fs';
54+
55+
import { chmod } from 'node:fs';
56+
57+
import { fail } from 'assert/strict';
58+
59+
// 'node-imports/no-node-import': ['error', { allowList: ['path'] }],
60+
import { chmod } from 'fs';
61+
62+
import { chmod } from 'node:fs';
63+
64+
import { fail } from 'assert/strict';
65+
```
66+
67+
Examples of **correct** code:
68+
69+
```ts
70+
// 'node-imports/no-node-import': 'error',
71+
import { item } from 'non-core-module';
72+
73+
import { item } from '../util';
74+
75+
// 'node-imports/no-node-import': ['error', { allowList: ['fs'] }],
76+
import { chmod } from 'fs';
77+
78+
import { chmod } from 'node:fs';
79+
```

0 commit comments

Comments
 (0)