Lint and Format Code with ESLint (VS Code)
- Firs initialize a project with
npm init
.
npm init --yes
- this will create a
package.json
file. that will contain all the dependencies of the project.
Then install ESLint.
npm install eslint --save-dev
- Initialize ESLint.
./node_modules/.bin/eslint --init
- Select the following options:
? How would you like to use ESLint? To check syntax, find problems, and enforce code style
? What type of modules does your project use? CommonJS (require/exports)
? Which framework does your project use? None of these
? Does your project use TypeScript? No
? Where does your code run? Browser
? How would you like to define a style for your project? Use a popular style guide
? Which style guide do you want to follow? Airbnb:
? What format do you want your config file to be in? JSON
Formatting on Save
- use the command
Ctrl+Shift+P
to open the command palette. - search Preferences: Open Workspace Settings (JSON).
- add the following code to the settings.json file.
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript"]
}