ESLint 新手入门
ESLint 是一个用于识别和报告在 ECMAScript/JavaScript 代码中发现的模式的工具,其目标是使代码更加一致并避免错误。
🌐 ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
ESLint 完全插件化。每一条规则都是一个插件,你可以在运行时添加更多。你还可以添加社区插件、配置和解析器以扩展 ESLint 的功能。
🌐 ESLint is completely pluggable. Every single rule is a plugin and you can add more at runtime. You can also add community plugins, configurations, and parsers to extend the functionality of ESLint.
先决条件
🌐 Prerequisites
要使用 ESLint,你必须安装并使用支持 SSL 的 Node.js(^20.19.0、^22.13.0 或 >=24)。如果你使用的是官方 Node.js 发行版,SSL 总是内置的。
🌐 To use ESLint, you must have Node.js (^20.19.0, ^22.13.0, or >=24) installed and built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.)
如果你使用 ESLint 的 TypeScript 类型定义,则需要 TypeScript 5.3 或更高版本。
🌐 If you use ESLint’s TypeScript type definitions, TypeScript 5.3 or later is required.
快速开始
🌐 Quick start
你可以使用以下命令安装和配置 ESLint:
🌐 You can install and configure ESLint using this command:
npm
npm init @eslint/config@latest
yarn
yarn create @eslint/config
pnpm
pnpm create @eslint/config@latest
bun
bun create @eslint/config@latest
如果你想使用托管在 npm 上的特定可共享配置,你可以使用 --config 选项并指定包名称:
🌐 If you want to use a specific shareable config that is hosted on npm, you can use the --config option and specify the package name:
npm
# use `eslint-config-xo` shared config - npm 7+
npm init @eslint/config@latest -- --config eslint-config-xo
yarn
# use `eslint-config-xo` shared config - npm 7+
yarn create @eslint/config -- --config eslint-config-xo
pnpm
# use `eslint-config-xo` shared config - npm 7+
pnpm create @eslint/config@latest -- --config eslint-config-xo
bun
# use `eslint-config-xo` shared config - npm 7+
bun create @eslint/config@latest -- --config eslint-config-xo
注意: npm init @eslint/config 假设你已经有一个 package.json 文件。如果没有,确保事先运行 npm init 或 yarn init。
之后,你可以在任何文件或目录上运行 ESLint,如下所示:
🌐 After that, you can run ESLint on any file or directory like this:
npm
npx eslint yourfile.js
yarn
yarn dlx eslint yourfile.js
pnpm
pnpm dlx eslint yourfile.js
bun
bunx eslint yourfile.js
配置
🌐 Configuration
注意: 如果你使用的是 9.0.0 之前的版本,请参阅 迁移指南。
当你运行 npm init @eslint/config 时,你将被询问一系列问题,以确定你如何使用 ESLint 以及应包含哪些选项。在回答这些问题后,你的目录中将创建一个 eslint.config.js(或 eslint.config.mjs)文件。
🌐 When you run npm init @eslint/config, you’ll be asked a series of questions to determine how you’re using ESLint and what options should be included. After answering these questions, you’ll have an eslint.config.js (or eslint.config.mjs) file created in your directory.
例如,其中一个问题是“你的代码在哪里运行?”如果你选择“浏览器”,那么你的配置文件将包含在网页浏览器中找到的全局变量的定义。下面是一个示例:
🌐 For example, one of the questions is “Where does your code run?” If you select “Browser” then your configuration file will contain the definitions for global variables found in web browsers. Here’s an example:
import { defineConfig } from "eslint/config";
import globals from "globals";
import js from "@eslint/js";
export default defineConfig([
{ files: ["**/*.js"], languageOptions: { globals: globals.browser } },
{ files: ["**/*.js"], plugins: { js }, extends: ["js/recommended"] },
]);
"js/recommended" 配置确保 rules page 上标记为推荐的所有规则都会被启用。或者,你可以使用其他人创建的配置,通过在 npmjs.com 上搜索 “eslint-config” 来找到。除非你从共享配置扩展或在配置中显式启用规则,否则 ESLint 不会对你的代码进行检查。
🌐 The "js/recommended" configuration ensures all of the rules marked as recommended on the rules page will be turned on. Alternatively, you can use configurations that others have created by searching for “eslint-config” on npmjs.com. ESLint will not lint your code unless you extend from a shared configuration or explicitly turn rules on in your configuration.
你可以通过定义一个带有 rules 键的新对象来单独配置规则,如以下示例所示:
🌐 You can configure rules individually by defining a new object with a rules key, as in this example:
import { defineConfig } from "eslint/config";
import js from "@eslint/js";
export default defineConfig([
{ files: ["**/*.js"], plugins: { js }, extends: ["js/recommended"] },
{
rules: {
"no-unused-vars": "warn",
"no-undef": "warn",
},
},
]);
"no-unused-vars" 和 "no-undef" 是 ESLint 中 规则 的名称。第一个值是规则的错误级别,可以是以下这些值之一:
🌐 The names "no-unused-vars" and "no-undef" are the names of rules in ESLint. The first value is the error level of the rule and can be one of these values:
"off"or0- 关闭规则"warn"or1- 将规则作为警告打开(不影响退出代码)"error"or2- 打开规则作为错误(退出代码将为 1)
三个错误级别允许你细粒度地控制 ESLint 如何应用规则(有关更多配置选项和详细信息,请参阅配置文档)。
🌐 The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the configuration docs).
全局安装
🌐 Global Install
也可以使用 npm install eslint --global 将 ESLint 全局安装,而不是本地安装。然而,这并不推荐,即使你全局安装了 ESLint,你使用的任何插件或可共享配置仍必须本地安装。
🌐 It is also possible to install ESLint globally, rather than locally, using npm install eslint --global. However, this is not recommended, and any plugins or shareable configs that you use must still be installed locally even if you install ESLint globally.
手动设置
🌐 Manual Set Up
你还可以在项目中手动设置 ESLint。
🌐 You can also manually set up ESLint in your project.
在开始之前,你必须已经有一个 package.json 文件。如果没有,确保先运行 npm init 或 yarn init 来创建该文件。
🌐 Before you begin, you must already have a package.json file. If you don’t, make sure to run npm init or yarn init to create the file beforehand.
- 在你的项目中安装 ESLint 包:
npm
npm install --save-dev eslint@latest @eslint/js@latest
yarn
yarn add --dev eslint@latest @eslint/js@latest
pnpm
pnpm add --save-dev eslint@latest @eslint/js@latest
bun
bun add --dev eslint@latest @eslint/js@latest
-
添加一个
eslint.config.js文件:# Create JavaScript configuration file touch eslint.config.js -
将配置添加到
eslint.config.js文件。请参考 配置 ESLint 文档 了解如何添加规则、自定义配置、插件等。import { defineConfig } from "eslint/config"; import js from "@eslint/js"; export default defineConfig([ { files: ["**/*.js"], plugins: { js, }, extends: ["js/recommended"], rules: { "no-unused-vars": "warn", "no-undef": "warn", }, }, ]); -
使用 ESLint CLI 进行代码检查:
npm
npx eslint project-dir/ file.js
yarn
yarn dlx eslint project-dir/ file.js
pnpm
pnpm dlx eslint project-dir/ file.js
bun
bunx eslint project-dir/ file.js
有关可用 CLI 选项的更多信息,请参阅 命令行接口。
🌐 For more information on the available CLI options, refer to Command Line Interface.
下一步
🌐 Next Steps