| 1 | # import/enforce-node-protocol-usage
|
|---|
| 2 |
|
|---|
| 3 | 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Enforce either using, or omitting, the `node:` protocol when importing Node.js builtin modules.
|
|---|
| 8 |
|
|---|
| 9 | ## Rule Details
|
|---|
| 10 |
|
|---|
| 11 | This rule enforces that builtins node imports are using, or omitting, the `node:` protocol.
|
|---|
| 12 |
|
|---|
| 13 | Determining whether a specifier is a core module depends on the node version being used to run `eslint`.
|
|---|
| 14 | This version can be specified in the configuration with the [`import/node-version` setting](../../README.md#importnode-version).
|
|---|
| 15 |
|
|---|
| 16 | Reasons to prefer using the protocol include:
|
|---|
| 17 |
|
|---|
| 18 | - the code is more explicitly and clearly referencing a Node.js built-in module
|
|---|
| 19 |
|
|---|
| 20 | Reasons to prefer omitting the protocol include:
|
|---|
| 21 |
|
|---|
| 22 | - some tools don't support the `node:` protocol
|
|---|
| 23 | - the code is more portable, because import maps and automatic polyfilling can be used
|
|---|
| 24 |
|
|---|
| 25 | ## Options
|
|---|
| 26 |
|
|---|
| 27 | The rule requires a single string option which may be one of:
|
|---|
| 28 |
|
|---|
| 29 | - `'always'` - enforces that builtins node imports are using the `node:` protocol.
|
|---|
| 30 | - `'never'` - enforces that builtins node imports are not using the `node:` protocol.
|
|---|
| 31 |
|
|---|
| 32 | ## Examples
|
|---|
| 33 |
|
|---|
| 34 | ### `'always'`
|
|---|
| 35 |
|
|---|
| 36 | ❌ Invalid
|
|---|
| 37 |
|
|---|
| 38 | ```js
|
|---|
| 39 | import fs from 'fs';
|
|---|
| 40 | export { promises } from 'fs';
|
|---|
| 41 | // require
|
|---|
| 42 | const fs = require('fs/promises');
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | ✅ Valid
|
|---|
| 46 |
|
|---|
| 47 | ```js
|
|---|
| 48 | import fs from 'node:fs';
|
|---|
| 49 | export { promises } from 'node:fs';
|
|---|
| 50 | import * as test from 'node:test';
|
|---|
| 51 | // require
|
|---|
| 52 | const fs = require('node:fs/promises');
|
|---|
| 53 | ```
|
|---|
| 54 |
|
|---|
| 55 | ### `'never'`
|
|---|
| 56 |
|
|---|
| 57 | ❌ Invalid
|
|---|
| 58 |
|
|---|
| 59 | ```js
|
|---|
| 60 | import fs from 'node:fs';
|
|---|
| 61 | export { promises } from 'node:fs';
|
|---|
| 62 | // require
|
|---|
| 63 | const fs = require('node:fs/promises');
|
|---|
| 64 | ```
|
|---|
| 65 |
|
|---|
| 66 | ✅ Valid
|
|---|
| 67 |
|
|---|
| 68 | ```js
|
|---|
| 69 | import fs from 'fs';
|
|---|
| 70 | export { promises } from 'fs';
|
|---|
| 71 |
|
|---|
| 72 | // require
|
|---|
| 73 | const fs = require('fs/promises');
|
|---|
| 74 |
|
|---|
| 75 | // This rule will not enforce not using `node:` protocol when the module is only available under the `node:` protocol.
|
|---|
| 76 | import * as test from 'node:test';
|
|---|
| 77 | ```
|
|---|
| 78 |
|
|---|
| 79 | ## When Not To Use It
|
|---|
| 80 |
|
|---|
| 81 | If you don't want to consistently enforce using, or omitting, the `node:` protocol when importing Node.js builtin modules.
|
|---|