Detalhes do pacote

eslint-plugin-n

eslint-community18.5mMIT17.21.3

Additional ESLint's rules for Node.js

eslint, eslintplugin, eslint-plugin, node

readme (leia-me)

eslint-plugin-n

forked from eslint-plugin-node v11.1.0. as the original repository seems no longer maintained.

npm version Downloads Build Status

Additional ESLint rules for Node.js

🎨 Playground

online-playground

💿 Install & Usage

npm install --save-dev eslint eslint-plugin-n
Version Supported Node.js Supported ESLint Version Status
17.x `^18.18.0 \ \ ^20.9.0 \ \ >=21.1.0` >=8.23.0 🏃‍♂️actively maintained
16.x >=16.0.0 >=7.0.0 ⚠️EOL
15.x >=12.22.0 >=7.0.0 ⚠️EOL

Note: It recommends a use of the "engines" field of package.json. The "engines" field is used by n/no-unsupported-features/* rules.

eslint.config.js (requires eslint>=v8.23.0)

const nodePlugin = require("eslint-plugin-n")

module.exports = [
    nodePlugin.configs["flat/recommended-script"],
    {
        rules: {
            "n/exports-style": ["error", "module.exports"]
        }
    }
]

To setup without the recommended configs, you'll need to add the plugin:

const nodePlugin = require("eslint-plugin-n")

module.exports = [
    {
        plugins: {n: nodePlugin},
        rules: {
            "n/exports-style": ["error", "module.exports"]
        }
    }
]

.eslintrc.json (legacy example)

{
    "extends": ["eslint:recommended", "plugin:n/recommended"],
    "parserOptions": {
        "ecmaVersion": 2021
    },
    "rules": {
        "n/exports-style": ["error", "module.exports"]
    }
}

To setup without the recommended rules you'll need to add the plugin:

{
    "parserOptions": {
        "ecmaVersion": 2021
    },
    "plugins": ["n"],
    "rules": {
        "n/exports-style": ["error", "module.exports"]
    }
}

package.json (An example)

{
    "name": "your-module",
    "version": "1.0.0",
    "type": "commonjs",
    "engines": {
        "node": ">=8.10.0"
    }
}

Configured Node.js version range

The rules get the supported Node.js version range from the following, falling back to the next if unspecified:

  1. Rule configuration version
  2. ESLint shared setting node.version
  3. package.json [engines] field
  4. >=16.0.0

If you omit the [engines] field, this rule chooses >=16.0.0 as the configured Node.js version since 16 is the maintained lts (see also Node.js Release Working Group).

For Node.js packages, using the [engines] field is recommended because it's the official way to indicate support:

{
    "name": "your-module",
    "version": "1.0.0",
    "engines": {
        "node": ">=16.0.0"
    }
}

For Shareable Configs or packages with a different development environment (e.g. pre-compiled, web package, etc.), you can configure ESLint with settings.node.version to specify support.

📖 Rules

💼 Configurations enabled in.\ 🟢 Set in the recommended-module configuration.\ ✅ Set in the recommended-script configuration.\ 🔧 Automatically fixable by the --fix CLI option.\ ❌ Deprecated.

Name Description 💼 🔧
callback-return require return statements after callbacks
exports-style enforce either module.exports or exports 🔧
file-extension-in-import enforce the style of file extensions in import declarations 🔧
global-require require require() calls to be placed at top-level module scope
handle-callback-err require error handling in callbacks
hashbang require correct usage of hashbang 🟢 ✅ 🔧
no-callback-literal enforce Node.js-style error-first callback pattern is followed
no-deprecated-api disallow deprecated APIs 🟢 ✅
no-exports-assign disallow the assignment to exports 🟢 ✅
no-extraneous-import disallow import declarations which import extraneous modules 🟢 ✅
no-extraneous-require disallow require() expressions which import extraneous modules 🟢 ✅
no-hide-core-modules disallow third-party modules which are hiding core modules
no-missing-import disallow import declarations which import non-existence modules 🟢 ✅
no-missing-require disallow require() expressions which import non-existence modules 🟢 ✅
no-mixed-requires disallow require calls to be mixed with regular variable declarations
no-new-require disallow new operators with calls to require
no-path-concat disallow string concatenation with __dirname and __filename
no-process-env disallow the use of process.env
no-process-exit disallow the use of process.exit() 🟢 ✅
no-restricted-import disallow specified modules when loaded by import declarations
no-restricted-require disallow specified modules when loaded by require
no-sync disallow synchronous methods
no-top-level-await disallow top-level await in published modules
no-unpublished-bin disallow bin files that npm ignores 🟢 ✅
no-unpublished-import disallow import declarations which import private modules 🟢 ✅
no-unpublished-require disallow require() expressions which import private modules 🟢 ✅
no-unsupported-features/es-builtins disallow unsupported ECMAScript built-ins on the specified version 🟢 ✅
no-unsupported-features/es-syntax disallow unsupported ECMAScript syntax on the specified version 🟢 ✅
no-unsupported-features/node-builtins disallow unsupported Node.js built-in APIs on the specified version 🟢 ✅
prefer-global/buffer enforce either Buffer or require("buffer").Buffer
prefer-global/console enforce either console or require("console")
prefer-global/process enforce either process or require("process")
prefer-global/text-decoder enforce either TextDecoder or require("util").TextDecoder
prefer-global/text-encoder enforce either TextEncoder or require("util").TextEncoder
prefer-global/url enforce either URL or require("url").URL
prefer-global/url-search-params enforce either URLSearchParams or require("url").URLSearchParams
prefer-node-protocol enforce using the node: protocol when importing Node.js builtin modules. 🔧
prefer-promises/dns enforce require("dns").promises
prefer-promises/fs enforce require("fs").promises
process-exit-as-throw require that process.exit() expressions use the same code path as throw 🟢 ✅
shebang require correct usage of hashbang 🔧

🔧 Configs

Name
🟢 recommended-module
recommended-script

About each config:

  • recommended: Considers both CommonJS and ES Modules. If "type":"module" field existed in package.json then it considers files as ES Modules. Otherwise it considers files as CommonJS. In addition, it considers *.mjs files as ES Modules and *.cjs files as CommonJS.
  • recommended-module: Considers all files as ES Modules.
  • recommended-script: Considers all files as CommonJS.
  • flat/all: enables all of the rules shipped with the package. This configuration is not recommended for production use because it may change with every minor and major version. Use at your own risk.

These preset configs:

  • enable no-process-exit rule because the official document does not recommend a use of process.exit().
  • enable plugin rules indicated by emojis in the rules table.
  • add {ecmaVersion: 2021} and etc into parserOptions.
  • add proper globals into globals.
  • add this plugin into plugins.

👫 FAQ

  • Q: The no-missing-import / no-missing-require rules don't work with nested folders in SublimeLinter-eslint
  • A: See context.getFilename() in rule returns relative path in the SublimeLinter-eslint FAQ.

  • Q: How to use the flat eslint config with mixed commonjs and es modules?

  • A: You can use the new exported flat config flat/mixed-esm-and-cjs, an example:
const nodePlugin = require("eslint-plugin-n");

module.exports = [
  ...nodePlugin.configs["flat/mixed-esm-and-cjs"],
  {
    rules: {
      "n/exports-style": ["error", "module.exports"],
    },
  },
]

🚥 Semantic Versioning Policy

eslint-plugin-n follows semantic versioning and ESLint's Semantic Versioning Policy.

  • Patch release (intended to not break your lint build)
    • A bug fix in a rule that results in it reporting fewer errors.
    • Improvements to documentation.
    • Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage.
    • Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone).
  • Minor release (might break your lint build)
    • A bug fix in a rule that results in it reporting more errors.
    • A new rule is created.
    • A new option to an existing rule is created.
    • An existing rule is deprecated.
  • Major release (likely to break your lint build)
    • A support for old Node version is dropped.
    • A support for old ESLint version is dropped.
    • An existing rule is changed in it reporting more errors.
    • An existing rule is removed.
    • An existing option of a rule is removed.
    • An existing config is updated.

Deprecated rules follow ESLint's deprecation policy.

📰 Changelog

❤️ Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

Development Tools

  • npm test runs tests and measures coverage.
  • npm run coverage shows the coverage result of npm test command.
  • npm run clean removes the coverage result of npm test command.

changelog (log de mudanças)

Changelog

17.21.3 (2025-07-28)

🩹 Fixes

  • no-missing-import: Support data imports (#465) (69ea12b)

17.21.2 (2025-07-27)

🧹 Chores

17.21.1 (2025-07-27)

🩹 Fixes

  • ci: Ignore type test failures for types present in ESLint 8 that we removed in ESLint 9.31.0 (#460) (76f8150)
  • no-restricted-require: Handle .. paths (#458) (34719e8)

17.21.0 (2025-07-03)

🌟 Features

🩹 Fixes

  • correctly handle version ranges in prefer-node-protocol (#454) (d60439d)

🧹 Chores

  • no-sync: remove @typescript-eslint/utils (#449) (45e2803)

17.20.0 (2025-06-12)

🌟 Features

  • no-top-level-await: add support for await using (#447) (88dea92)

17.19.0 (2025-06-02)

🌟 Features

  • add no-top-level-await rule (#440) (a500a48)
  • add support for ignoring sync methods from certain locations (#424) (ef94e49)

17.18.0 (2025-05-09)

🌟 Features

  • Allow to use tryExtensions in the rule no-unpublished-import (#429) (002ac9c)
  • no-deprecated-api: Add support for process.getBuiltinModule() (#435) (fca7cf4)
  • node-builtins: Add support for process.getBuiltinModule() (#433) (d4c7831)
  • prefer-global: Add support for process.getBuiltinModule() (#436) (83aa3a7)
  • prefer-node-protocol: Add support for process.getBuiltinModule() (#430) (9e97d8e)
  • prefer-promises: Add support for process.getBuiltinModule() (#437) (6f5e81d)

🩹 Fixes

  • false negatives for https in prefer-node-protocol, and false negatives for node:sqlite in node-builtins (#432) (bb21bd0)

17.17.0 (2025-03-26)

🌟 Features

  • allow-modules: include virtual: in the modules pattern (#425) (a109793)
  • node-builtin: Add support for import.meta properties (#420) (76fc219)

🩹 Fixes

  • no-unpublished: allowModules option was disabled when using TS alias (#423) (9ae39fb)
  • type errors (#427) (8e3c290)

17.16.2 (2025-03-04)

🩹 Fixes

  • Revert "feat: add support for ignoring sync methods from certain locations" (#416) (0779e2f)

17.16.1 (2025-03-02)

🩹 Fixes

🧹 Chores

  • package: explicitly declare js module type (#410) (9f09931)

17.16.0 (2025-02-23)

🌟 Features

  • add support for ignoring sync methods from certain locations (#392) (5544f20)

🩹 Fixes

  • False-positive no-extraneous-import when using the tsconfig > paths alias import (#408) (f486492)

🧹 Chores

17.15.1 (2024-12-20)

🩹 Fixes

  • Promise.withResolvers is supported since node 22.11 (#398) (c5bcb3a)

17.15.0 (2024-12-10)

🌟 Features

  • no-unsupported: support node 22.12.0 (#393) (af4f774)
  • resolve: allow overriding enhanced-resolve's options (#384) (1466bec)

🩹 Fixes

  • no-unsupported: Correctly handle recursive objects on a per module basis (#396) (db384d1)

17.14.0 (2024-11-21)

🌟 Features

17.13.2 (2024-11-15)

🩹 Fixes

  • no-missing-require: handle multiple resolvePaths (#383) (df6ad2a)

17.13.1 (2024-11-07)

🩹 Fixes

  • exported / referenced plugin same instance (#380) (3c45b67)

17.13.0 (2024-11-05)

🌟 Features

  • no-unsupported: support Node 20.18.0 (#374) (d39d99a)

🩹 Fixes

  • no-unsupported: fix node:test module (#378) (0b228dd)

🧹 Chores

17.12.0 (2024-10-30)

🌟 Features

  • no-unsupported: Support node 23.0.0 and 22.10.0 (#358) (0fd0350)
  • no-unsupported: Support node 23.1.0 (#370) (06d60ae)

🩹 Fixes

  • no-unsupported: getCallSite is experimental (#363) (d15c63a)
  • no-unsupported: support missing process.features (#362) (9552a4a)
  • update dependencies (#365) (bf34ca5)

🧹 Chores

  • Improve typescript types and strictness (#367) (18cdd53)

17.11.1 (2024-10-09)

🩹 Fixes

  • no-deprecated-api: dedeprecate process.nextTick (#350) (dd889ab)

17.11.0 (2024-10-09)

🌟 Features

  • no-missing-import: Add ignoreTypeImport options (#344) (e022aba)
  • no-process-env: Allow users to exclude specific variables (#345) (b16a475)
  • Update no-unsupported to node v22.9.0 (#342) (87fb484)

🩹 Fixes

  • no-missing-import: Ignore node builtins in package.json imports (#346) (148e47e)
  • no-missing-import: Resolve tsconfig paths relative to the tsconfig (#343) (6cd7954)

📚 Documentation

  • Explain the transitive dependency case for no-extraneous-* (#347) (8c0a2cc)

17.10.3 (2024-09-18)

🩹 Fixes

  • Use our data set to work out if a module is a node module (#338) (6a1b2c5)

📚 Documentation

  • process-exit-as-throw: update wording (#323) (e5e758e)

17.10.2 (2024-08-05)

🩹 Fixes

17.10.1 (2024-07-26)

🩹 Fixes

17.10.0 (2024-07-26)

🌟 Features

📚 Documentation

🧹 Chores

  • upgrade compatible deps (8f6f11d)

17.9.0 (2024-06-14)

🌟 Features

  • Add flag ignorePrivate to no-unpublished-x rules (#298) (0609431)

17.8.1 (2024-06-06)

🩹 Fixes

  • hashbang + eslint v8 compat issue (e82974f)
  • hashbang + eslint v8 compat issue (#290) (e82974f)

17.8.0 (2024-06-05)

🌟 Features

  • node-builtin: Add node 22.2.0 support (#282) (5221c40)

🩹 Fixes

  • Allow for misconfigured default exports (#288) (92e18b5)

🧹 Chores

17.7.0 (2024-05-14)

🌟 Features

  • hashbang: Add support to map extensions to executables (#278) (3fd7639)
  • node-builtin: Add node 20.13.0, 22.0.0, and 22.1.0 support (#276) (4a685c0)

🩹 Fixes

  • node-builtins: Remove "node:" prefix from "ignores" message (#277) (704f0b9)

📚 Documentation

  • node-builtins: Specify that only static properties are supported (#272) (735a520)
  • Provide an example with eslint-plugin-n to Playground (#275) (cb8ffa6)

17.6.0 (2024-05-10)

🌟 Features

  • Add support for ignoring experemental features (#269) (c046376)

📚 Documentation

17.5.1 (2024-05-07)

🩹 Fixes

  • Add supported version to Buffer constructor (#266) (030f51b)

17.5.0 (2024-05-07)

🌟 Features

  • import-target: Add resolution error reason (#264) (982a723)
  • node-builtins: Add node globals (#261) (9466731)

🩹 Fixes

🧹 Chores

  • update dependency markdownlint-cli to ^0.40.0 (#263) (1e41e7c)

17.4.0 (2024-04-30)

🌟 Features

  • no-deprecated-api support removed api (#240) (36fd35d)

🩹 Fixes

  • unsupported-features/node-builtins-modules version comparation (#257) (5c67787)

🧹 Chores

17.3.0 (2024-04-24)

🌟 Features

🩹 Fixes

  • unsupported-features/node-builtins-modules range compare (#252) (d50ae85)

🧹 Chores

17.2.1 (2024-04-15)

🩹 Fixes

🧹 Chores

  • update dependency @typescript-eslint/typescript-estree to v7 (#236) (a0b45ee)
  • update dependency eslint-plugin-eslint-plugin to v6 (#237) (7addf99)

17.2.0 (2024-04-10)

🌟 Features

  • no-missing-imports: add tryExtensions option (#228) (ae5329c)
  • no-unsupported-features: ✨ Update to node v20.12.0/v21.7.0 (#229) (a8d0539)

🩹 Fixes

  • stream/promises is stable (02a264e)
  • no-unsupported-features: stream/promises is stable (#235) (02a264e), closes #234

17.1.0 (2024-04-09)

🌟 Features

📚 Documentation

17.0.0 (2024-04-08)

💥 Breaking changes:

  • feat!: drop eslint v7 & node.js < 18 (#161) (41ceed7)
  • feat!: Start using enhanced-resolve to improve ts support (#139) (dc9f473)
  • rename rule shebang => hashbang, deprecate rule shebang (#198)

Features

  • typescript (jsdoc) checking and definition generation (#169) (6d8ed14)
  • rename rule shebang => hashbang, deprecate rule shebang (#198) (cefdb1c), closes #196
  • shebang: add support for env's split-string option (#195) (b383b49)
  • Update ES Syntax (#189) (4778ae8)
  • feat: Update es-builtins (#174) (fbc9e7b)
  • feat(no-unsupported-features): Update to v21.6.1 of node (#180) (d24f645)
  • feat: Add n/prefer-node-protocol rule (#183) (88d1c37)
  • feat(shebang): Add options to ignore unpublished files (#172) (5609abb)

Bugfixes

  • prefer-node-protocol: not first target (#204) (caab777)
  • prefer-node-protocol: continue on version range check (#206) (14d2ea9)
  • prefer-node-prefix: pass moduleName (#203)
  • no-unsupported-features: Remove use of static as a variable (#190) (e31d868)
  • fix: Remove require("util") import in import-target (#181) (d32eff3)

Chores

  • use ts-ignore-import to lighten the dependencies (#219) (fb0aaae)
  • update dependency @typescript-eslint/parser to v7 (#207) (0b8aeb3)
  • update dependency husky to v9 (#208) (e84d47b)
  • Enable global strict eslint rule (#191) (99fe387)
  • Migrate to manifest config (#192) (c8a87f3)
  • docs: improve wording of file-extension-in-import docs (#110) (3f178ab)
  • build: run test on eslint pre-releases (#171) (77de809)
  • ci: Make release-please publish pre-releases (#186) (4b12cdc)
  • chore: update dependency globals to v14 (#185) (9930101)
  • chore: update dependency markdownlint-cli to ^0.39.0 (#179) (cd5cbbb)
  • chore: Merge supported and backport properties (#177) (5d1cb98)
  • chore: npm run format (#175) (17e658e)
  • chore!: remove "n/no-unsupported-features" #140 (#173) (372b283)
  • chore: update dependency minimatch to v9 (#167) (5ad657c)
  • chore: add release-please (#170) (fc77da2)
  • chore: update dependency @typescript-eslint/parser to v6 (#166) (4265094)
  • chore: update dependency markdownlint-cli to ^0.38.0 (#149) (3fd61be)
  • chore: update dependency release-it to v17 (#168) (1c91e05)
  • chore: upgrade prettier v3 (#165) (bbfde8d)

17.0.0-8 (2024-04-08)

Chores

  • use ts-ignore-import to lighten the dependencies (#219) (fb0aaae)

17.0.0-7 (2024-04-07)

Features

  • typescript (jsdoc) checking and definition generation (#169) (6d8ed14)

Bug Fixes

  • change peer dependencies to allow eslint v9 (#216) (5e82d7f)

17.0.0-6 (2024-03-25)

Bug Fixes

  • prefer-node-protocol: not first target (#204) (caab777)
  • prefer-node-protocol: continue on version range check (#206) (14d2ea9)

Chores

  • update dependency @typescript-eslint/parser to v7 (#207) (0b8aeb3)
  • update dependency husky to v9 (#208) (e84d47b)

17.0.0-5 (2024-03-19)

⚠ BREAKING CHANGES

  • prefer-node-prefix: pass moduleName (#203)

Features

Bug Fixes

  • explicitly support ESLint 9.0.0 pre-releases (#200) (a5eaa9c)

Documentation

  • Remove text "Node does not support modules yet" (#202) (5abca5b)

17.0.0-4 (2024-03-06)

⚠ BREAKING CHANGES

  • rename rule shebang => hashbang, deprecate rule shebang (#198)

Features

  • rename rule shebang => hashbang, deprecate rule shebang (#198) (cefdb1c), closes #196
  • shebang: add support for env's split-string option (#195) (b383b49)
  • Update ES Syntax (#189) (4778ae8)

Bug Fixes

  • no-unsupported-features: Remove use of static as a variable (#190) (e31d868)

Chores