]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Getting Started with ESLint |
2 | ||
3 | 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. In many ways, it is similar to JSLint and JSHint with a few exceptions: | |
4 | ||
5 | * ESLint uses [Espree](https://github.com/eslint/espree) for JavaScript parsing. | |
6 | * ESLint uses an AST to evaluate patterns in code. | |
7 | * ESLint is completely pluggable, every single rule is a plugin and you can add more at runtime. | |
8 | ||
9 | ## Installation and Usage | |
10 | ||
609c276f | 11 | Prerequisites: [Node.js](https://nodejs.org/en/) (`^12.22.0`, `^14.17.0`, or `>=16.0.0`) built with SSL support. (If you are using an official Node.js distribution, SSL is always built in.) |
eb39fafa DC |
12 | |
13 | You can install ESLint using npm or yarn: | |
14 | ||
34eeec05 | 15 | ```sh |
eb39fafa DC |
16 | npm install eslint --save-dev |
17 | ||
18 | # or | |
19 | ||
20 | yarn add eslint --dev | |
21 | ``` | |
22 | ||
6f036462 | 23 | You should then set up a configuration file, and the easiest way to do that is to use the `--init` flag: |
eb39fafa | 24 | |
34eeec05 | 25 | ```sh |
eb39fafa | 26 | $ npx eslint --init |
6f036462 TL |
27 | |
28 | # or | |
29 | ||
30 | $ yarn run eslint --init | |
eb39fafa DC |
31 | ``` |
32 | ||
6f036462 TL |
33 | **Note:** `--init` assumes you have a `package.json` file already. If you don't, make sure to run `npm init` or `yarn init` beforehand. |
34 | ||
eb39fafa DC |
35 | After that, you can run ESLint on any file or directory like this: |
36 | ||
34eeec05 | 37 | ```sh |
eb39fafa | 38 | $ npx eslint yourfile.js |
6f036462 TL |
39 | |
40 | # or | |
41 | ||
42 | $ yarn run eslint yourfile.js | |
eb39fafa DC |
43 | ``` |
44 | ||
45 | 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 be installed locally in either case. | |
46 | ||
47 | ## Configuration | |
48 | ||
49 | **Note:** If you are coming from a version before 1.0.0 please see the [migration guide](migrating-to-1.0.0.md). | |
50 | ||
51 | After running `eslint --init`, you'll have a `.eslintrc.{js,yml,json}` file in your directory. In it, you'll see some rules configured like this: | |
52 | ||
53 | ```json | |
54 | { | |
55 | "rules": { | |
56 | "semi": ["error", "always"], | |
57 | "quotes": ["error", "double"] | |
58 | } | |
59 | } | |
60 | ``` | |
61 | ||
62 | The names `"semi"` and `"quotes"` are the names of [rules](/docs/rules) in ESLint. The first value is the error level of the rule and can be one of these values: | |
63 | ||
64 | * `"off"` or `0` - turn the rule off | |
65 | * `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code) | |
66 | * `"error"` or `2` - turn the rule on as an error (exit code will be 1) | |
67 | ||
5422a9cc | 68 | The three error levels allow you fine-grained control over how ESLint applies rules (for more configuration options and details, see the [configuration docs](configuring/)). |
eb39fafa DC |
69 | |
70 | Your `.eslintrc.{js,yml,json}` configuration file will also include the line: | |
71 | ||
72 | ```json | |
73 | { | |
74 | "extends": "eslint:recommended" | |
75 | } | |
76 | ``` | |
77 | ||
78 | Because of this line, all of the rules marked "(recommended)" on the [rules page](/docs/rules) will be turned on. Alternatively, you can use configurations that others have created by searching for "eslint-config" on [npmjs.com](https://www.npmjs.com/search?q=eslint-config). ESLint will not lint your code unless you extend from a shared configuration or explicitly turn rules on in your configuration. | |
79 | ||
80 | --- | |
81 | ||
82 | ## Next Steps | |
83 | ||
5422a9cc | 84 | * Learn about [advanced configuration](configuring/) of ESLint. |
eb39fafa DC |
85 | * Get familiar with the [command line options](command-line-interface.md). |
86 | * Explore [ESLint integrations](integrations.md) into other tools like editors, build systems, and more. | |
87 | * Can't find just the right rule? Make your own [custom rule](/docs/developer-guide/working-with-rules.md). | |
88 | * Make ESLint even better by [contributing](/docs/developer-guide/contributing/). |