|
| 1 | +--- |
| 2 | +title: "Linting JavaScript in Visual Studio" |
| 3 | +description: Learn how to lint JavaScript in Visual Studio |
| 4 | +ms.date: 09/29/2022 |
| 5 | +ms.topic: how-to |
| 6 | +ms.devlang: javascript |
| 7 | +author: mikejo5000 |
| 8 | +ms.author: mikejo |
| 9 | +manager: jmartens |
| 10 | +ms.technology: vs-javascript |
| 11 | +dev_langs: |
| 12 | + - JavaScript |
| 13 | +ms.workload: |
| 14 | + - "nodejs" |
| 15 | +monikerRange: '>= vs-2022' |
| 16 | +--- |
| 17 | + |
| 18 | +# Linting JavaScript in Visual Studio |
| 19 | + |
| 20 | +Linting JavaScript and TypeScript in Visual Studio is powered by [ESLint](https://eslint.org/). If you're new to ESLint, you can begin by checking their [documentation](https://eslint.org/docs/latest/user-guide/getting-started). |
| 21 | + |
| 22 | +## Enabling linting support |
| 23 | + |
| 24 | +To enable linting support in Visual Studio 2022 or later versions, enable the **Enable ESLint** setting in **Tools > Options > Text Editor > JavaScript/TypeScript > Linting**. |
| 25 | + |
| 26 | +:::image type="content" source="media/vs-2022/linting-tools-options.png" alt-text="Screenshot of linting Tools Options page."::: |
| 27 | + |
| 28 | +In the options page, you can also modify the set of files that you want to lint. By default, all file extensions that can be linted (*.js*, *.jsx*, *.ts*, *.tsx*) will be linted. |
| 29 | + |
| 30 | +You can override these options in some project types, like the [standalone React project templates](../javascript/tutorial-create-react-app.md). In these projects, you can override the settings from the **Tools > Options** page using project properties: |
| 31 | + |
| 32 | +:::image type="content" source="media/vs-2022/linting-project-properties.png" alt-text="Screenshot of linting project properties."::: |
| 33 | + |
| 34 | +## Installing ESLint dependencies |
| 35 | + |
| 36 | +Once linting is enabled, the necessary dependencies need to be installed. Dependencies include the [ESLint `npm` package](https://www.npmjs.com/package/eslint) and other plugins applicable to your project. This package can be installed locally in each project where you want to enable linting, or you can install it globally using `npm install -g eslint`. However, a global installation isn't recommended because plugins and shareable configs always need to be installed locally. |
| 37 | + |
| 38 | +Depending on the files you want to lint, other [ESLint plugins](https://eslint.org/docs/latest/user-guide/configuring/plugins) may be needed. For example, you may need [TypeScript ESLint](https://typescript-eslint.io/), which enables ESLint to run on TypeScript code and includes rules that are specific to the extra type information. |
| 39 | + |
| 40 | +When ESLint is enabled but the ESLint `npm` package isn't found, a gold bar is displayed that allows you to install ESLint as a local `npm` development dependency. |
| 41 | + |
| 42 | +:::image type="content" source="media/vs-2022/no-package-goldbar.png" alt-text="Screenshot of Install ESLint gold bar."::: |
| 43 | + |
| 44 | +Similarly, when an *.eslintrc* file isn't found, a gold bar is displayed to run a configuration wizard that will install the plugins applicable to the current project. |
| 45 | + |
| 46 | +:::image type="content" source="media/vs-2022/no-config-goldbar.png" alt-text="Screenshot of Run ESLint wizard gold bar."::: |
| 47 | + |
| 48 | +## Disabling linting rules and auto-fixes |
| 49 | + |
| 50 | +You can disable linting errors on a [specific line or file](https://eslint.org/docs/latest/user-guide/configuring/rules#disabling-rules). You can disable the errors by using the Quick Actions lightbulb menu: |
| 51 | + |
| 52 | +:::image type="content" source="media/vs-2022/code-actions.png" alt-text="Screenshot of linting Quick Actions."::: |
| 53 | + |
| 54 | +The following illustration shows the result if you disable a linting error for the selected line of code. |
| 55 | + |
| 56 | +:::image type="content" source="media/vs-2022/disabled-rule.png" alt-text="Screenshot of disabled linting rule."::: |
| 57 | + |
| 58 | +In addition, auto-fix code actions allow you to apply an auto-fix to address the respective linting error. |
| 59 | + |
| 60 | +## Troubleshooting |
| 61 | + |
| 62 | +You can open the **ESLint Language Extension** pane in the Output window to see any error messages or other logs that might explain the problem. |
0 commit comments