eslint vs js-beautify vs prettier vs standard
JavaScript 代码质量与格式化工具架构对比
eslintjs-beautifyprettierstandard类似的npm包:

JavaScript 代码质量与格式化工具架构对比

eslintprettierjs-beautifystandard 都是前端工程中用于维护代码一致性的核心工具,但它们解决的问题层面不同。eslint 是一个可插拔的 Linting 工具,主要用于发现代码中的逻辑错误和潜在问题,同时也支持风格检查;prettier 是一个固执的代码格式化程序,专注于通过解析 AST 来统一代码风格,不关心逻辑错误;standard 是基于 ESLint 封装的零配置风格指南,强制推行一套特定的规范;js-beautify 是一个较老的格式化工具,支持 JS、HTML 和 CSS 的美化,但在现代前端工作流中逐渐被 Prettier 取代。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
eslint027,3112.91 MB11112 天前MIT
js-beautify08,973982 kB4631 年前MIT
prettier051,9658.6 MB1,4051 天前MIT
standard029,430164 kB1282 年前MIT

ESLint vs Prettier vs Standard vs js-beautify: 代码质量与格式化深度对比

在前端工程化体系中,eslintprettierjs-beautifystandard 都扮演着代码规范守门员的角色,但它们的底层原理和适用场景有明显区别。理解这些差异能帮助架构师为团队选择最合适的工具组合。让我们从核心定位、配置方式、修复能力和工作流集成四个方面进行深度对比。

🎯 核心定位:代码质量 vs 代码风格

eslint 的核心是 Linting(代码检查)。

  • 它通过静态分析查找代码中的逻辑错误和潜在 Bug。
  • 它也检查风格,但主要目的是保证代码质量。
// eslint: 检查逻辑错误
// 配置示例 .eslintrc.js
module.exports = {
  rules: {
    'no-unused-vars': 'error', // 报错:未使用的变量
    'eqeqeq': 'warn'           // 警告:使用 == 而不是 ===
  }
};

prettier 的核心是 Formatting(格式化)。

  • 它不关心代码逻辑是否正确,只关心代码长得是否一致。
  • 它通过解析代码生成 AST,然后按统一规则重新打印。
// prettier: 统一风格
// 配置示例 .prettierrc
{
  "semi": true,               // 强制使用分号
  "singleQuote": true,        // 强制使用单引号
  "printWidth": 80            // 每行最大字符数
}

standard 的核心是零配置规范。

  • 它基于 ESLint,但预设了一套不可更改的规则。
  • 它的目标是消除配置文件的维护成本。
// standard: 零配置规范
// 通常在 package.json 中声明
{
  "name": "my-project",
  "standard": {
    "globals": [ "jQuery" ]   // 仅允许少量覆盖
  }
}

js-beautify 的核心是文本美化。

  • 它更像是一个文本处理工具,基于规则缩进和换行。
  • 它对代码语义的理解不如 Prettier 深。
// js-beautify: 基础美化
// 配置示例 .jsbeautifyrc
{
  "indent_size": 2,           // 缩进大小
  "brace_style": "collapse",  // 大括号风格
  "preserve-newlines": true   // 保留空行
}

🛠️ 配置方式:灵活定制 vs 约定优于配置

配置管理的复杂度直接影响团队的维护成本。

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

🔄 自动修复:智能重写 vs 文本替换

自动修复能力能显著提升开发效率,但不同工具的实现深度不同。

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 专注于格式化修复。

  • 它会完全重写代码格式,保证 100% 一致。
  • 使用 --write 标志。
// prettier: 自动格式化
// 原始代码
const x=1

// 运行: prettier --write file.js
// 修复后
const x = 1;

standard 继承 ESLint 的修复能力。

  • 它使用 ESLint 的修复引擎。
  • 使用 --fix 标志。
// standard: 自动修复
// 运行: standard --fix
// 行为与 eslint --fix 类似,但规则集固定

js-beautify 进行文本级修复。

  • 它主要处理缩进和空格。
  • 使用 -r (replace) 标志。
// js-beautify: 自动美化
// 运行: js-beautify -r file.js
// 修复后:仅调整缩进和换行,不改变语法结构

🚀 工作流集成:CLI 与 编辑器插件

在实际开发中,工具需要融入编辑器和 CI 流程。

eslint 拥有最丰富的生态。

  • 支持所有主流编辑器(VS Code, WebStorm)。
  • 支持 eslint-loadereslint-webpack-plugin
// package.json: 集成脚本
{
  "scripts": {
    "lint": "eslint src/**",
    "lint:fix": "eslint src/** --fix"
  }
}

prettier 通常作为保存时触发。

  • 编辑器插件在保存文件时自动运行。
  • 也支持 CLI 用于 CI 检查。
// package.json: 集成脚本
{
  "scripts": {
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  }
}

standard 简化了集成步骤。

  • 因为无配置,CI 脚本非常简单。
  • 编辑器插件直接启用即可。
// package.json: 集成脚本
{
  "scripts": {
    "test": "standard",
    "fix": "standard --fix"
  }
}

js-beautify 集成相对基础。

  • 主要通过 CLI 或编辑器插件调用。
  • 较少用于现代化的 Git Hooks 流程。
// package.json: 集成脚本
{
  "scripts": {
    "beautify": "js-beautify -r src/**/*.js"
  }
}

📊 总结对比表

特性eslintprettierstandardjs-beautify
主要用途代码质量 + 风格代码风格代码风格 (零配置)代码美化
配置难度高 (灵活)低 (固执)极低 (无配置)中 (传统)
逻辑检查✅ 支持❌ 不支持✅ 支持 (基于 ESLint)❌ 不支持
修复能力部分修复完全格式化部分修复文本美化
现代推荐度⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

💡 架构师建议

eslint + prettier 是现代前端的标准组合

  • eslint 负责捕捉 Bug 和逻辑问题。
  • prettier 负责所有风格相关的格式化。
  • 这种分工最清晰,维护成本最低。

standard 适合特定场景

  • 如果团队不想花时间讨论或维护 ESLint 配置,且能接受 StandardJS 的风格,它是一个高效的启动方案。
  • 但要注意,它的规则可能过于严格,难以适应所有项目。

js-beautify 建议仅在遗留项目中使用

  • 在新项目中,Prettier 的 AST 解析能力能更好地处理现代 JavaScript 语法(如可选链、空值合并)。
  • js-beautify 更适合处理非 JS 文件(如 HTML/CSS)的简单美化,如果 Prettier 不支持的话。

最终结论:对于大多数专业前端团队,首选 eslint 配合 prettier。这种组合提供了最强大的质量控制能力和最一致的代码风格,同时保持了配置的灵活性。

如何选择: eslint vs js-beautify vs prettier vs standard

  • eslint:

    选择 eslint 如果你需要深度定制代码规则,或者需要检查除了风格之外的逻辑错误(如未使用的变量、潜在的空指针)。它是大型项目和质量要求严格团队的首选,通常与 Prettier 配合使用。

  • js-beautify:

    选择 js-beautify 如果你正在维护旧项目,或者需要格式化 Prettier 不支持的特定文件类型(如某些模板语言)。在新项目中,通常建议优先考虑 Prettier。

  • prettier:

    选择 prettier 如果团队希望消除关于代码风格的争论,并且想要一个“开箱即用”的格式化体验。它最适合与 ESLint 搭配,由 Prettier 负责风格,ESLint 负责质量。

  • standard:

    选择 standard 如果团队希望零配置启动,并且愿意接受社区约定的标准风格,不想花费时间维护 ESLint 配置文件。它适合小型团队或快速原型开发。

eslint的README

npm version Downloads Build Status
Open Collective Backers Open Collective Sponsors

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:

  • ESLint uses Espree for JavaScript parsing.
  • ESLint uses an AST to evaluate patterns in code.
  • ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime.

Table of Contents

  1. Installation and Usage
  2. Configuration
  3. Version Support
  4. Code of Conduct
  5. Filing Issues
  6. Frequently Asked Questions
  7. Releases
  8. Security Policy
  9. Semantic Versioning Policy
  10. ESM Dependencies
  11. License
  12. Team
  13. Sponsors
  14. Technology Sponsors

Installation and Usage

Prerequisites

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.

npm Installation

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

pnpm Installation

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.

Configuration

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).

Version Support

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.

Code of Conduct

ESLint adheres to the OpenJS Foundation Code of Conduct.

Filing Issues

Before filing an issue, please be sure to read the guidelines for what you're reporting:

Frequently Asked Questions

Does ESLint support JSX?

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.

Does Prettier replace ESLint?

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.

What ECMAScript versions does ESLint support?

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.

What about experimental features?

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.

Which Node.js versions does ESLint support?

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:

  1. The most recent maintenance release of Node.js
  2. The lowest minor version of the Node.js LTS release that includes the features the ESLint team wants to use.
  3. The Node.js Current release

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.

Where to ask for help?

Open a discussion or stop by our Discord server.

Why doesn't ESLint lock dependency versions?

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.

Releases

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.

Security Policy

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.

Semantic Versioning 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:

  • Patch release (intended to not break your lint build)
    • A bug fix in a rule that results in ESLint reporting fewer linting errors.
    • A bug fix to the CLI or core (including formatters).
    • 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 ESLint reporting more linting errors.
    • A new rule is created.
    • A new option to an existing rule that does not result in ESLint reporting more linting errors by default.
    • A new addition to an existing rule to support a newly-added language feature (within the last 12 months) that will result in ESLint reporting more linting errors by default.
    • An existing rule is deprecated.
    • A new CLI capability is created.
    • New capabilities to the public API are added (new classes, new methods, new arguments to existing methods, etc.).
    • A new formatter is created.
    • eslint:recommended is updated and will result in strictly fewer linting errors (e.g., rule removals).
  • Major release (likely to break your lint build)
    • eslint:recommended is updated and may result in new linting errors (e.g., rule additions, most rule option updates).
    • A new option to an existing rule that results in ESLint reporting more linting errors by default.
    • An existing formatter is removed.
    • Part of the public API is removed or changed in an incompatible way. The public API includes:
      • Rule schemas
      • Configuration schema
      • Command-line options
      • Node.js API
      • Rule, formatter, parser, plugin APIs

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.

ESM Dependencies

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.

License

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.

Team

These folks keep the project moving and are resources for help.

Technical Steering Committee (TSC)

The people who manage releases, review feature requests, and meet regularly to ensure ESLint is properly maintained.

Nicholas C. Zakas's Avatar
Nicholas C. Zakas
Francesco Trotta's Avatar
Francesco Trotta
Milos Djermanovic's Avatar
Milos Djermanovic

Reviewers

The people who review and implement new features.

唯然's Avatar
唯然
Nitin Kumar's Avatar
Nitin Kumar

Committers

The people who review and fix bugs and help triage issues.

fnx's Avatar
fnx
Josh Goldberg ✨'s Avatar
Josh Goldberg ✨
Sweta Tanwar's Avatar
Sweta Tanwar
Tanuj Kanti's Avatar
Tanuj Kanti
lumir's Avatar
lumir
Pixel998's Avatar
Pixel998

Website Team

Team members who focus specifically on eslint.org

Amaresh  S M's Avatar
Amaresh S M
Harish's Avatar
Harish
Percy Ma's Avatar
Percy Ma

Sponsors

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.

Platinum Sponsors

Automattic

Gold Sponsors

Qlty Software

Silver Sponsors

Vite Liftoff StackBlitz

Bronze Sponsors

Cybozu SAP CrawlJobs Syntax Depot Icons8 Discord GitBook HeroCoders Citadel AI

Technology Sponsors

Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.

Netlify Algolia 1Password