eslint vs prettier vs stylelint vs tslint
前端代码质量与风格自动化工具选型
eslintprettierstylelinttslint类似的npm包:

前端代码质量与风格自动化工具选型

eslintprettierstylelinttslint 都是用于维护代码质量和一致性的开发工具,但它们的侧重点不同。eslint 主要用于检查 JavaScript 和 TypeScript 代码中的逻辑错误和风格问题。prettier 专注于代码格式化,确保所有代码看起来都一样。stylelint 专门用于检查 CSS 预处理器和样式表中的错误。tslint 曾经是 TypeScript 的专用检查工具,但现已废弃,官方建议迁移至 eslint

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
eslint027,2812.91 MB1129 天前MIT
prettier051,8938.6 MB1,4881 个月前MIT
stylelint011,469954 kB1254 天前MIT
tslint05,906-16 年前Apache-2.0

前端代码质量与风格自动化工具深度对比

在构建现代前端应用时,保持代码质量和风格一致至关重要。eslintprettierstylelinttslint 是生态中最常见的四个工具,但它们解决的问题不同。理解它们的区别能帮你搭建高效的工程化体系。

🎯 核心定位:检查逻辑 vs 格式化 vs 样式检查

eslint 专注于 JavaScript 和 TypeScript 的代码质量。

  • 它能发现逻辑错误,比如未定义的变量。
  • 它能强制执行编码规范,比如禁止使用 var
  • 它通过插件系统支持几乎所有前端框架。
// eslint: .eslintrc.js 配置示例
module.exports = {
  parser: '@typescript-eslint/parser',
  rules: {
    'no-unused-vars': 'error',
    'eqeqeq': 'warn'
  }
};

prettier 专注于代码格式的统一。

  • 它不关心代码逻辑是否正确。
  • 它只关心缩进、换行、引号等外观问题。
  • 它支持 JS、TS、CSS、JSON 等多种语言。
// prettier: .prettierrc 配置示例
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "printWidth": 80
}

stylelint 专注于样式表的质量。

  • 它能检查 CSS 语法错误。
  • 它能强制命名规范,比如 BEM 风格。
  • 它能避免使用过时的属性。
// stylelint: .stylelintrc.json 配置示例
{
  "rules": {
    "color-no-invalid-hex": true,
    "selector-class-pattern": "^[a-z][a-zA-Z0-9]*$"
  }
}

tslint 曾经是 TypeScript 的专用检查器。

  • 它只能检查 TypeScript 代码。
  • 它已停止维护,功能不再更新。
  • 它的配置方式与 eslint 类似,但已不再推荐使用。
// tslint: tslint.json 配置示例 (已废弃)
{
  "rules": {
    "no-unused-variable": true,
    "semicolon": [true, "always"]
  }
}

🛠️ 实际工作流中的协作

在现代工程实践中,这些工具通常一起使用,而不是二选一。

eslint + prettier 是标准组合。

  • eslint 负责保证代码不出错。
  • prettier 负责保证代码好看。
  • 使用 eslint-config-prettier 关闭 eslint 中所有与格式相关的规则,避免冲突。
// eslint: 关闭与 prettier 冲突的规则
module.exports = {
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier' // 必须放在最后,以覆盖格式规则
  ]
};

stylelint 独立运行或集成到构建流程。

  • 它通常在 CSS 构建阶段运行。
  • 它可以与 prettier 共存,prettier 格式化 CSS 结构,stylelint 检查 CSS 质量。
# 命令行运行示例
npx eslint src/
npx prettier --write src/
npx stylelint "src/**/*.css"

⚠️ 重要警告:TSLint 已废弃

tslint 不再适用于新项目。

  • 官方仓库已归档,不再接受新功能。
  • TypeScript 团队推荐迁移到 eslint
  • 继续使用 tslint 会导致技术债务。
# 迁移命令示例
# 不再安装 tslint
# 改为安装 eslint 及相关插件
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

如果你正在维护旧项目,应制定计划迁移到 eslinttypescript-eslint 项目提供了完整的规则映射,可以帮助自动化这一过程。

📊 功能对比总结

特性eslintprettierstylelinttslint
主要用途代码质量与逻辑检查代码格式化样式表检查TypeScript 检查 (已废弃)
支持语言JS, TS, JSX, Vue 等JS, TS, CSS, JSON 等CSS, SCSS, LessTypeScript 仅
配置复杂度高 (可高度定制)低 ( opinionated )中 (规则丰富)中 (已停止更新)
维护状态✅ 活跃维护✅ 活跃维护✅ 活跃维护❌ 已废弃
推荐场景所有 JS/TS 项目所有项目含 CSS 的项目无 (请迁移)

💡 最佳实践建议

对于新项目:

  • 必选 eslint 配合 @typescript-eslint
  • 必选 prettier 用于格式化。
  • 必选 stylelint 如果项目包含 CSS 模块。
  • 忽略 tslint,不要安装。

对于旧项目:

  • 如果正在使用 tslint,计划迁移到 eslint
  • 使用 tslint-to-eslint-config 工具辅助转换配置。
  • 逐步替换规则,确保 CI 流程不被打断。

配置整合:

  • 在 VS Code 中安装对应插件,实现保存时自动修复。
  • 在 CI/CD 流程中加入检查步骤,阻止不符合规范的代码合并。
// package.json 脚本示例
{
  "scripts": {
    "lint": "eslint . --ext .ts,.tsx",
    "format": "prettier --write .",
    "lint:css": "stylelint \"src/**/*.css\""
  }
}

🏁 结论

这四个工具并非竞争关系,而是互补关系。eslintstylelint 负责质量门禁,prettier 负责消除风格争论。tslint 已成为历史,应尽快从技术栈中移除。合理组合使用这些工具,能显著提升团队开发效率和代码可维护性 — 这是现代前端工程化的基石。

如何选择: eslint vs prettier vs stylelint vs tslint

  • eslint:

    选择 eslint 作为 JavaScript 和 TypeScript 项目的主要代码检查工具。它拥有最丰富的插件生态,能够发现潜在的逻辑错误、未使用的变量以及复杂度过高的代码。对于新项目,建议配合 @typescript-eslint 插件使用,以获得最佳的 TypeScript 支持。

  • prettier:

    选择 prettier 来处理所有代码的格式化工作。它 opinionated 的设计理念意味着你不需要争论缩进是 2 格还是 4 格,只需提交代码即可。它与 eslint 互补,前者管逻辑质量,后者管代码长相。

  • stylelint:

    选择 stylelint 来管理 CSS、SCSS 或 Less 样式文件。它能防止写错属性名、强制颜色格式统一以及避免特定选择器的使用。对于大型项目或设计系统,它是保持样式一致性的必要工具。

  • tslint:

    不要在新项目中选择 tslint。该工具已正式废弃,维护团队明确建议所有用户迁移到 eslint。继续使用 tslint 会导致无法获得最新的 TypeScript 特性支持,且未来可能面临兼容性问题。

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 TestMu AI Open Source Office (Formerly LambdaTest)

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