Below are the steps to starting using eslint a node project.
Step 1. Installing Eslint:
You have option to install locally or globally:
npm install eslint --save-dev
To integrate within vscode, install eslint extension.
Step 2. Add linting rules:
Create a .eslintrc file in the project folder, this will make your rules project specific.
To know about various rules click here.
You can also simply follow the best practices rules around:
- JS standard: https://standardjs.com/
We are going to use JS standard as a sharable config, detailed here:
npm install --save-dev eslint-config-standard eslint-plugin-standard eslint-plugin-promise eslint-plugin-import eslint-plugin-node
"extends": "standard" to the .eslintrc file.
Step 3. Configure VS code:
Add the below to user setting in vs code, this will enable auto formatting as per eslint rules that you have defined. And reload your project.
Now you are all set.
- Define environment in eslint:
- Define globals in eslint:
- You can also add git pre-commit hooks in your project which checks for eslint errors. And if any found would not allow the dev to commit.
Thanks for reading to so far. If you like the article, please give a thumbs up! :D