Everyone wants to write code in a fast bug-free way without thinking about its style most of the time. That’s why in this post I will talk about configuring ESLint and Prettier in an Angular project.
How does ESLint help?
By statically analyzing our code, ESLint can find problems and also suggest us fixes for them. And it can do better than that, it can fix our code automatically (who doesn’t want that?).
Install and configure ESLint
In this section, I will explain how to install ESLint in an Angular project and also configure it to better align with the Angular style guide and community standards.
Open the terminal and install ESLint schematics using this command:
ng add @angular-eslint/schematics
That was it. Now we have ESLint installed and also configured thanks to **ng add**
command provided by the Angular-ESLint team.
Example error and how ESLint helps to fix it:
We can also run this command in terminal:
ng lint --fix
to fix all the fixable bugs in the project.
Install and configure Prettier
Even if we have ESLint watching our code for bugs, we also need a tool to better style and format it. That’s where Prettier comes into play.
Prettier is an opinionated code formatter that helps us beautify code in a standardized way every time we save the code.
Open terminal and type:
npm install prettier --save-dev
or if you’re using yarn :
yarn add prettier -D
Then we need to add .prettierrc.json and .prettierignore files in our root project directory.
Inside .prettierignore it’s better to add whatever we have inside .gitignore file.
Then we can run this command inside our project to format it:
npx prettier --write .
Inside .prettierrc.json we can change the default settings by overriding them.
The settings I use most of the time are this:
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"semi": true,
"bracketSpacing": true,
"arrowParens": "avoid",
"trailingComma": "es5",
"bracketSameLine": true,
"printWidth": 80
}
That’s it about Prettier. But we are not finished.
There are times where ESLint and Prettier have different opinions about code formatting and style. That’s why we need to tweak this part. More info here.
Configure Prettier to be used as an ESLint plugin
For ESLint and Prettier to play well together, we need to run Prettier as an ESLint plugin. This way we can just call ng lint --fix
and ESLint will fix bugs but also format the code.
Open terminal and type:
npm install prettier-eslint eslint-config-prettier eslint-plugin-prettier — save-dev
or if you're using yarn:
yarn add prettier-eslint eslint-config-prettier eslint-plugin-prettier -D
Now we need to edit the .eslintrc.json file to include the prettier plugin.
{
"root": true,
"overrides": [
{
"files": ["*.ts"],
"extends": [
...
"plugin:prettier/recommended"
],
},
// NOTE: WE ARE NOT APPLYING PRETTIER IN THIS OVERRIDE, ONLY @ANGULAR-ESLINT/TEMPLATE
{
"files": ["*.html"],
"extends": ["plugin:@angular-eslint/template/recommended"],
"rules": {}
},
// NOTE: WE ARE NOT APPLYING @ANGULAR-ESLINT/TEMPLATE IN THIS OVERRIDE, ONLY PRETTIER
{
"files": ["*.html"],
"excludedFiles": ["*inline-template-*.component.html"],
"extends": ["plugin:prettier/recommended"],
"rules": {
// NOTE: WE ARE OVERRIDING THE DEFAULT CONFIG TO ALWAYS SET THE PARSER TO ANGULAR (SEE BELOW)
"prettier/prettier": ["error", { "parser": "angular" }]
}
}
]
}
VSCode and Webstorm shortucts
That was it. We’re done with the configuration part.
After we edit a file, we want to format it and then save. That’s what we will configure now for both VS Code and Webstorm.
First make sure you have ESLint and Prettier plugin installed. WebStorm has support of of the box for both.
For VS Code we need to add this lines to settings.json:
{
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": false
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.formatOnSave": false
},
}
For Webstorm:
We need to check: Run eslint --fix on Actions On Save settings page:
Here you can find the steps summed up: Angular ESLint & Prettier Configuration
How to automate all these configurations?
As you saw, there are a lot of packages that you should install and configure. And I can tell you confidently that there is a way to manage all these automatically. NX is the answer.
What is NX?
NX is the next generation build system with first-class monorepo support and powerful integrations.
If we migrate our Angular app to an Nx monorepo (there is a straightforward migration path) we get all those configurations for free out-of-the-box.
But what makes NX special are some crazy features like computation caching, smart rebuilds, distributed task execution, remote build caching, powerful code generators, editor plugins, etc.
*That was it. Thank you for reading! See you on the next one.
*
Top comments (7)
The command to add eslint plugins has an error in it when copy pasting.
looks like word formatted your double dash to a special word character, it should be this:
Cheers! 🚀
Got an error:
NOT SUPPORTED: keyword "id", use "$id" for schema ID
Resolved adding "@angular-eslint/schematics": "14.4.0", to devDependencies in package.json. Use same major version as Angular.
Wow, very helpful, thanks.
Thx a lot for this article ❤️
Is this still the right approach with angular@17 (especially for the
.eslintrc.json
file)?on VSCode you should also add this option:
"eslint.format.enable": true,
what if I need to apply typescript rules like in ts-lint so I need to make sure there's no " any " type in the project
Thank you