npm install --save-dev --save-exact prettier
in the repo you’re working on so on a per project basisecho {}> .prettierrc.json
to create a file that is needed to recognise this repo uses prettier- create a
.prettierignore
file withtouch .prettierignore
and put files to be ignored in it based on your ESLintignore and/or gitignore file (gitignore would be recommended to copy over) - install Prettier extension in VSCode to be able to format using a shortcut or set it up to run on save etc
For more info on setting up a gitignore file: https://www.freecodecamp.org/news/gitignore-file-how-to-ignore-files-and-folders-in-git/
- run
npm init
ornpm init --yes
in the repo/project you’ll be working on. This is to get a package.json file in there if it's not there yet.
the
--yes
addition to answer the prompts with yes automatically
npm init @eslint/config
to hit two birds with one stone (intitialize and creating the config file)- configure the resulting eslintrc.js file first by the prompts and/or review https://www.digitalocean.com/community/tutorials/linting-and-formatting-with-eslint-in-vs-code for prompt decisions and later adjusting of config
- install the ESLint VSCode extension for visual clues during programming and maybe even configure it to do it's magic on each save automatically, also explained on the above DigitalOcean lesson
npm install --save-dev eslint-config-prettier
in the same repo to make ESLint and Prettier work nicely together without conflict- add "prettier" to the "extends" array in your .eslintrc.* file at the end of the file
example:
"extends": [
"airbnb-base",
"prettier"
],
npx eslint-config-prettier path/to/main.js
to see if there are conflicts
Now to manually use prettier for formatting you can use the shortcut shift+alt+f
or use the command npx prettier --write <filename>
To see all the errors and warnings for ESLint you can use the command npx eslint <filename>
but they're also shown in the VSCode Problems section
- set up ESLint to automatically fix syntax and formatting issues upon save by following Step 4 and 5 of this article: https://www.digitalocean.com/community/tutorials/linting-and-formatting-with-eslint-in-vs-code