Lint Lightning Web Components
Linting provides errors about malformed code while you edit. VS Code enforces Salesforce’s ESLint rules.
The active LTS release of Node.js built with SSL support.
You can either install ESLint for a new project, or an existing one.
If you create an SFDX project using the sf project:generate command, your project contains a package.json file with the ESLint plugin already included.
-
To install the ESLint plugin and other dependencies, run
npm installin your project directory. -
Configure the ESLint plugin rules.
-
To run linting, you must have components in your project. To start linting, run
npm run lint:lwc.
-
Verify that your project has a
package.jsonwith these configurations. If yourpackage.jsondoesn't have the configurations, runnpm install eslint @salesforce/eslint-config-lwc --save-dev. -
If your project doesn't have the
package.jsonfile, copy it and add it to your project directory. -
Verify that your project has this
.eslintignorefile. This file specifies which files not to lint. If your project doesn't have it, add it to your project directory. -
Verify that your project has this
.eslintrc.jsonfile. This file specifies your linting configuration level. If your project doesn't have it, add it to your project directory. -
To install the ESLint plugin and other dependencies, run
npm installon your project directory. -
To run linting, you must have components in your project. To start linting, run
npm run lint:lwc.
ESLint includes three configuration levels. The default level is @salesforce/eslint-config-lwc/recommended.
To change the configuration level, edit this line in the .eslintrc.json
-
@salesforce/eslint-config-lwc/baseThis configuration prevents common pitfalls with Lightning web components and enforces other Salesforce platform restrictions. -
@salesforce/eslint-config-lwc/recommendedThis configuration prevents common JavaScript pitfalls and enforces all best practices. -
@salesforce/eslint-config-lwc/extendedThis configuration restricts the use of some JavaScript language features that are sometimes slow in older browsers, such as IE11. To support new JavaScript syntax and language features on an older browser, the Lightning web components compiler transforms the Lightning web components modules.
For more details on the linting rules and using them individually, see the ESLint Plugin Github repository.
The "scripts" section of package.json includes some scripts already pre-configured to run ESLint. To add your own, see the npm documentation.
- For more information about configuring ESLint, see the ESLint User Guide.
- github.com/salesforce/eslint-plugin-lwc
- github.com/salesforce/eslint-config-lwc