Prettier and ESLint configuration for JavaScript & Vue projects
- Installation
- Usage with Vue 2/3
- Usage with Vue 3 & TypeScript
- Usage with JavaScript only
- Inheriting Prettier rules
- Linting your project with NPM scripts
- Enabling autofix on save for VS Code
- Install the packages to your project with the command below:
npm install -D eslint prettier@^2.7.1 @elibol/eslint-config eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue
- Create
.eslintrc.js
file in your project root if it doesn't exist - Pick the configuration you need from the list below and replace the content of the file with one of the examples below
- Create
prettier.config.js
file in your project root if it doesn't exist and replace the content with the example below
// .eslintrc.js
module.exports = {
root: true,
extends: ['@elibol/eslint-config'],
}
NOTE: that some rules might not work perfectly with Vue version 2. For those, feel free to override rules in
.eslintrc.js
file.
To be able to use TypeScript configuration, make sure to install dependencies below:
npm install -D @typescript-eslint/parser vue-eslint-parser @vue/eslint-config-typescript
// .eslintrc.js
module.exports = {
root: true,
extends: ['@elibol/eslint-config/vue3-typescript'],
}
If you would only need to import JavaScript rules but not Vue, then you can use the package as following:
module.exports = {
extends: ['@elibol/eslint-config/javascript'],
}
If this is the scenario, you don't need to install eslint-plugin-vue
either.
This package uses prettier by default. For the config to work properly, you need to inherit prettier
rules from the project.
To do that, replace content of your prettier.config.js
file with the code below
module.exports = require('@elibol/eslint-config/prettier.config')
NOTE: By default the package exports Vue configuration. So using
extends: ["@elibol/eslint-config"]
orextends: ["@elibol"]
will by default include JavaScript andeslint-plugin-vue
rules. If you want to use pure JavaScript or Vue TypeScript configuration, see below.
Add the scripts below to your package.json
file. Then you will be able to run
npm lint
oryarn lint
for running the linter drynpm lint:fix
oryarn lint:fix
to run the linter and fix errors/warnings (those that are fixable)
{
"scripts": {
"lint": "eslint \"**/*.{vue,ts,js}\"",
"lint-fix": "eslint --fix \"**/*.{vue,ts,js}\""
}
}
Add the settings below to your VSCode settings to run linter on every save
.vscode/settings.json
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}