This blog will guide you through setting up a nodejs project with eslint. Setting up eslint gives you the benefit to get the linting errors to maintain styling for your javascript project. you can also have auto-formatting enable for vs code on saving of the files.
Prerequisite:
- Node.js
- Github(optional)
Reference:
Eslint Git Project
Get started:
Create a new node.js project or use your existing node js project.
To create a new nodejs project type npm init and complete the project setup.
Install eslint as a dev dependency to your project.npm install eslint@7.32.0 --save-dev
Next setup eslintnpx eslint --init
This will ask a few questions regarding the setup for the eslint below is the reference configuration used during the setup, the configuration can be selected as per your preferences.
Install eslint extension on vs code
Once installed Edit the setting as json and add the following line to auto-format the code on save
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
vs code sample setting.json with eslint settings is as below:
{
"diffEditor.wordWrap": "on",
"editor.wordWrap": "on",
"editor.wordWrapColumn": 90,
"eslint.format.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.workingDirectories": [
],
"eslint.validate": [
"javascript"
],
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"editor.fontWeight": null,
"eslint.nodeEnv": ""
}
After completion of editing setting json file make a change in the code and save the file eslint will auto-format the code before saving.
Thank you for reading.