NodeJS + ESLint + Prettier - Simplest setup ever
Sabbir Siddiqui
Posted on August 23, 2021
Don't expect any fancy images in this post, let's get down to business. I'm just gonna hope you're using yarn because you should be.
yarn add eslint --dev
(from here)yarn run eslint --init
(also from above link)Follow whatever options your heart desires in the
eslint
setup prompts.Get rid of the
package-lock.json
cause likely theeslint
cli used npm to install something. tsk. Remove that file, and just runyarn
to make things right again.yarn add prettier eslint-config-prettier eslint-plugin-prettier @typescript-eslint/parser -D
(from here)Your repo should have an
.eslintrc.js
file by now that looks like this:
module.exports = {
env: {
browser: true,
es2021: true,
'jest/globals': true,
node: true,
},
extends: ['airbnb-base', 'eslint:recommended', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'jest', 'prettier'],
rules: {
semi: ['error', 'always'],
quotes: ['error', 'single'],
'import/extensions': [
'error',
'ignorePackages',
{
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
'import/no-dynamic-require': 0,
'global-require': 0,
'import/prefer-default-export': 0,
'no-underscore-dangle': 0,
'no-await-in-loop': 0,
'no-restricted-syntax': 0,
'no-return-await': 0,
'no-console': 0,
'prettier/prettier': [
'error',
{
trailingComma: 'es5',
singleQuote: true,
printWidth: 80,
tabWidth: 2,
endOfLine: 'lf',
arrowParens: 'always',
},
],
},
};
- And an
.eslintignore
that looks like this: (if not create one)
/node_modules
/reports
- Add a command in your
package.json
to run lint fixes
{
...
"scripts": {
...
"lintfix": "eslint src --fix --cache",
...
},
}
- If you want to use husky for pre-commit hooks, be my guest. You can check the site for instructions but here they are anyway:
a.
yarn add husky --save-dev
b.npx husky install
c.npx husky add .husky/pre-commit "yarn eslint && git add -A"
d. git add -A e. git commit -m "finally configured eslint and prettier and husky without any 🐄 💩
💖 💪 🙅 🚩
Sabbir Siddiqui
Posted on August 23, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.