eslint、prettier、js-beautify 和 standard 都是前端工程中用于维护代码一致性的核心工具,但它们解决的问题层面不同。eslint 是一个可插拔的 Linting 工具,主要用于发现代码中的逻辑错误和潜在问题,同时也支持风格检查;prettier 是一个固执的代码格式化程序,专注于通过解析 AST 来统一代码风格,不关心逻辑错误;standard 是基于 ESLint 封装的零配置风格指南,强制推行一套特定的规范;js-beautify 是一个较老的格式化工具,支持 JS、HTML 和 CSS 的美化,但在现代前端工作流中逐渐被 Prettier 取代。
在前端工程化体系中,eslint、prettier、js-beautify 和 standard 都扮演着代码规范守门员的角色,但它们的底层原理和适用场景有明显区别。理解这些差异能帮助架构师为团队选择最合适的工具组合。让我们从核心定位、配置方式、修复能力和工作流集成四个方面进行深度对比。
eslint 的核心是 Linting(代码检查)。
// eslint: 检查逻辑错误
// 配置示例 .eslintrc.js
module.exports = {
rules: {
'no-unused-vars': 'error', // 报错:未使用的变量
'eqeqeq': 'warn' // 警告:使用 == 而不是 ===
}
};
prettier 的核心是 Formatting(格式化)。
// prettier: 统一风格
// 配置示例 .prettierrc
{
"semi": true, // 强制使用分号
"singleQuote": true, // 强制使用单引号
"printWidth": 80 // 每行最大字符数
}
standard 的核心是零配置规范。
// standard: 零配置规范
// 通常在 package.json 中声明
{
"name": "my-project",
"standard": {
"globals": [ "jQuery" ] // 仅允许少量覆盖
}
}
js-beautify 的核心是文本美化。
// js-beautify: 基础美化
// 配置示例 .jsbeautifyrc
{
"indent_size": 2, // 缩进大小
"brace_style": "collapse", // 大括号风格
"preserve-newlines": true // 保留空行
}
配置管理的复杂度直接影响团队的维护成本。
eslint 提供极高的灵活性。
eslint:recommended)。# eslint: 初始化配置
npx eslint --init
# 生成 .eslintrc.js 或 eslint.config.js
prettier 配置简单且有限。
# prettier: 检查配置
npx prettier --check .
# 如果有 .prettierrc 则读取,否则使用默认值
standard 几乎不需要配置。
# standard: 运行检查
npx standard
# 自动读取 package.json 中的 standard 字段,无需单独文件
js-beautify 配置较为传统。
# js-beautify: 命令行指定配置
npx js-beautify -f file.js --indent-size 4
# 或读取 .jsbeautifyrc
自动修复能力能显著提升开发效率,但不同工具的实现深度不同。
eslint 可以修复部分问题。
--fix 标志。// eslint: 自动修复
// 原始代码
var x = 1
if(x==2){ console.log('hi') }
// 运行: eslint --fix file.js
// 修复后 (取决于规则)
var x = 1;
if (x == 2) { console.log('hi'); }
prettier 专注于格式化修复。
--write 标志。// prettier: 自动格式化
// 原始代码
const x=1
// 运行: prettier --write file.js
// 修复后
const x = 1;
standard 继承 ESLint 的修复能力。
--fix 标志。// standard: 自动修复
// 运行: standard --fix
// 行为与 eslint --fix 类似,但规则集固定
js-beautify 进行文本级修复。
-r (replace) 标志。// js-beautify: 自动美化
// 运行: js-beautify -r file.js
// 修复后:仅调整缩进和换行,不改变语法结构
在实际开发中,工具需要融入编辑器和 CI 流程。
eslint 拥有最丰富的生态。
eslint-loader 或 eslint-webpack-plugin。// package.json: 集成脚本
{
"scripts": {
"lint": "eslint src/**",
"lint:fix": "eslint src/** --fix"
}
}
prettier 通常作为保存时触发。
// package.json: 集成脚本
{
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}
standard 简化了集成步骤。
// package.json: 集成脚本
{
"scripts": {
"test": "standard",
"fix": "standard --fix"
}
}
js-beautify 集成相对基础。
// package.json: 集成脚本
{
"scripts": {
"beautify": "js-beautify -r src/**/*.js"
}
}
| 特性 | eslint | prettier | standard | js-beautify |
|---|---|---|---|---|
| 主要用途 | 代码质量 + 风格 | 代码风格 | 代码风格 (零配置) | 代码美化 |
| 配置难度 | 高 (灵活) | 低 (固执) | 极低 (无配置) | 中 (传统) |
| 逻辑检查 | ✅ 支持 | ❌ 不支持 | ✅ 支持 (基于 ESLint) | ❌ 不支持 |
| 修复能力 | 部分修复 | 完全格式化 | 部分修复 | 文本美化 |
| 现代推荐度 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ |
eslint + prettier 是现代前端的标准组合。
eslint 负责捕捉 Bug 和逻辑问题。prettier 负责所有风格相关的格式化。standard 适合特定场景。
js-beautify 建议仅在遗留项目中使用。
js-beautify 更适合处理非 JS 文件(如 HTML/CSS)的简单美化,如果 Prettier 不支持的话。最终结论:对于大多数专业前端团队,首选 eslint 配合 prettier。这种组合提供了最强大的质量控制能力和最一致的代码风格,同时保持了配置的灵活性。
选择 eslint 如果你需要深度定制代码规则,或者需要检查除了风格之外的逻辑错误(如未使用的变量、潜在的空指针)。它是大型项目和质量要求严格团队的首选,通常与 Prettier 配合使用。
选择 js-beautify 如果你正在维护旧项目,或者需要格式化 Prettier 不支持的特定文件类型(如某些模板语言)。在新项目中,通常建议优先考虑 Prettier。
选择 prettier 如果团队希望消除关于代码风格的争论,并且想要一个“开箱即用”的格式化体验。它最适合与 ESLint 搭配,由 Prettier 负责风格,ESLint 负责质量。
选择 standard 如果团队希望零配置启动,并且愿意接受社区约定的标准风格,不想花费时间维护 ESLint 配置文件。它适合小型团队或快速原型开发。
Website | Configure ESLint | Rules | Contribute to ESLint | Report Bugs | Code of Conduct | X | Discord | Mastodon | Bluesky
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code. In many ways, it is similar to JSLint and JSHint with a few exceptions:
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.)
If you use ESLint's TypeScript type definitions, TypeScript 5.3 or later is required.
You can install and configure ESLint using this command:
npm init @eslint/config@latest
After that, you can run ESLint on any file or directory like this:
npx eslint yourfile.js
To use ESLint with pnpm, we recommend setting up a .npmrc file with at least the following settings:
auto-install-peers=true
node-linker=hoisted
This ensures that pnpm installs dependencies in a way that is more compatible with npm and is less likely to produce errors.
You can configure rules in your eslint.config.js files as in this example:
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.js", "**/*.cjs", "**/*.mjs"],
rules: {
"prefer-const": "warn",
"no-constant-binary-expression": "error",
},
},
]);
The names "prefer-const" and "no-constant-binary-expression" 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" or 0 - turn the rule off"warn" or 1 - turn the rule on as a warning (doesn't affect exit code)"error" or 2 - turn the rule on as an error (exit code will be 1)The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the configuration docs).
The ESLint team provides ongoing support for the current version and six months of limited support for the previous version. Limited support includes critical bug fixes, security issues, and compatibility issues only.
ESLint offers commercial support for both current and previous versions through our partners, Tidelift and HeroDevs.
See Version Support for more details.
ESLint adheres to the OpenJS Foundation Code of Conduct.
Before filing an issue, please be sure to read the guidelines for what you're reporting:
Yes, ESLint natively supports parsing JSX syntax (this must be enabled in configuration). Please note that supporting JSX syntax is not the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using eslint-plugin-react if you are using React and want React semantics.
No, ESLint and Prettier have different jobs: ESLint is a linter (looking for problematic patterns) and Prettier is a code formatter. Using both tools is common, refer to Prettier's documentation to learn how to configure them to work well with each other.
ESLint has full support for ECMAScript 3, 5, and every year from 2015 up until the most recent stage 4 specification (the default). You can set your desired ECMAScript syntax and other settings (like global variables) through configuration.
ESLint's parser only officially supports the latest final ECMAScript standard. We will make changes to core rules in order to avoid crashes on stage 3 ECMAScript syntax proposals (as long as they are implemented using the correct experimental ESTree syntax). We may make changes to core rules to better work with language extensions (such as JSX, Flow, and TypeScript) on a case-by-case basis.
In other cases (including if rules need to warn on more or fewer cases due to new syntax, rather than just not crashing), we recommend you use other parsers and/or rule plugins. If you are using Babel, you can use @babel/eslint-parser and @babel/eslint-plugin to use any option available in Babel.
Once a language feature has been adopted into the ECMAScript standard (stage 4 according to the TC39 process), we will accept issues and pull requests related to the new feature, subject to our contributing guidelines. Until then, please use the appropriate parser and plugin(s) for your experimental feature.
ESLint updates the supported Node.js versions with each major release of ESLint. At that time, ESLint's supported Node.js versions are updated to be:
ESLint is also expected to work with Node.js versions released after the Node.js Current release.
Refer to the Quick Start Guide for the officially supported Node.js versions for a given ESLint release.
Open a discussion or stop by our Discord server.
Lock files like package-lock.json are helpful for deployed applications. They ensure that dependencies are consistent between environments and across deployments.
Packages like eslint that get published to the npm registry do not include lock files. npm install eslint as a user will respect version constraints in ESLint's package.json. ESLint and its dependencies will be included in the user's lock file if one exists, but ESLint's own lock file would not be used.
We intentionally don't lock dependency versions so that we have the latest compatible dependency versions in development and CI that our users get when installing ESLint in a project.
The Twilio blog has a deeper dive to learn more.
We have scheduled releases every two weeks on Friday or Saturday. You can follow a release issue for updates about the scheduling of any particular release.
ESLint takes security seriously. We work hard to ensure that ESLint is safe for everyone and that security issues are addressed quickly and responsibly. Read the full security policy.
ESLint follows semantic versioning. However, due to the nature of ESLint as a code quality tool, it's not always clear when a minor or major version bump occurs. To help clarify this for everyone, we've defined the following semantic versioning policy for ESLint:
eslint:recommended is updated and will result in strictly fewer linting errors (e.g., rule removals).eslint:recommended is updated and may result in new linting errors (e.g., rule additions, most rule option updates).According to our policy, any minor update may report more linting errors than the previous release (ex: from a bug fix). As such, we recommend using the tilde (~) in package.json e.g. "eslint": "~3.1.0" to guarantee the results of your builds.
Since ESLint is a CommonJS package, there are restrictions on which ESM-only packages can be used as dependencies.
Packages that are controlled by the ESLint team and have no external dependencies can be safely loaded synchronously using require(esm) and therefore used in any contexts.
For external packages, we don't use require(esm) because a package could add a top-level await and thus break ESLint. We can use an external ESM-only package only in case it is needed only in asynchronous code, in which case it can be loaded using dynamic import().
These policies don't apply to packages intended for our own usage only, such as eslint-config-eslint.
MIT License
Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
These folks keep the project moving and are resources for help.
The people who manage releases, review feature requests, and meet regularly to ensure ESLint is properly maintained.
![]() Nicholas C. Zakas |
![]() Francesco Trotta |
![]() Milos Djermanovic |
The people who review and implement new features.
![]() 唯然 |
![]() Nitin Kumar |
The people who review and fix bugs and help triage issues.
![]() fnx |
![]() Josh Goldberg ✨ |
![]() Sweta Tanwar |
![]() Tanuj Kanti |
![]() lumir |
Pixel998 |
Team members who focus specifically on eslint.org
![]() Amaresh S M |
![]() Harish |
![]() Percy Ma |
The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. Become a Sponsor to get your logo on our READMEs and website.