Using Prettier with Airbnb's ESLint config

July 31, 2019

What is Prettier and Airbnb’s ESlint configuration

Airbnb ESLint configuration is one of the most used ESLint configuration and Prettier is an opinionated code formatter with a handful options.

Both are very strong tools to make your code more readable, increases your code quality with consistency and prevents you from some serious mistakes in your code.

Adding modules

Make a directory, step into it, just initialise your project with npm init and install ESLint and Prettier along with a Prettier config and plugin for ESLint

$ npm install -D eslint prettier eslint-config-prettier eslint-plugin-prettier

-   prettier@1.18.2
-   eslint@6.1.0
-   eslint-plugin-prettier@3.1.0
-   eslint-config-prettier@6.0.0
    added 125 packages from 79 contributors and audited 182 packages in 7.427s
    found 0 vulnerabilities

Additionally you need the Airbnb ESLint configuration with its peer dependecies. Here I am using install-peerdeps from Nathan H. Leung.

$ npx install-peerdeps --dev eslint-config-airbnb

install-peerdeps v1.10.2
Installing peerdeps for eslint-config-airbnb@latest.
npm install eslint-config-airbnb@17.1.1 eslint@5.3.0 eslint-plugin-import@^2.18.0 eslint
-plugin-jsx-a11y@^6.2.3 eslint-plugin-react@^7.14.2 --save-dev

-   eslint-plugin-jsx-a11y@6.2.3
-   eslint-plugin-import@2.18.2
-   eslint-config-airbnb@17.1.1
-   eslint-plugin-react@7.14.3
-   eslint@5.3.0
    added 87 packages from 54 contributors, removed 14 packages, updated 24 packages and aud
    ited 540 packages in 9.653s
    found 0 vulnerabilities

SUCCESS eslint-config-airbnb
and its peerDeps were installed successfully.

Configure files

I am using .yaml-files for the configuration so create a .prettierrc.yaml and a .eslintrc.yaml. If you are using a *nix-like system just put this in your console:

$ echo "---
extends:
  - airbnb
  - prettier
plugins:
  - prettier
rules:
  prettier/prettier:
    - error" >> .eslintrc.yaml
$ echo "---
trailingComma: 'es5'
printWidth: 100
tabWidth: 4
useTabs: true
semi: false
singleQuote: true" >> .prettierrc.yaml

Now add two npm run scripts with json from Trent Mick.

$ npx json -I -f package.json -e 'this.scripts.lint="eslint --ext .jsx,.js src"'
npx: installed 1 in 0.89s
json: updated "package.json" in-place

You can run now your “linting” with npm run lint and find your errors.

$ npx json -I -f package.json -e 'this.scripts.fix="npm run lint -- --fix"'
npx: installed 1 in 0.926s
json: updated "package.json" in-place

This will “fix” and format your code with npm run fix. The “fix” script won’t fix your entire code (see Fixing problems in the ESLint documentation) but the most common things.

Try out your configuration

Add a really bad JavaScript file to your project:

$ mkdir src; echo "            function logTest (arg) {
const test =             \"\";

        if (test !=        \"\")
                console.log(test);

}

logTest('test');
" >> src/bad.js

And run npm run fix and you should see something like this:

$ npm run fix

> myproject@1.0.0 fix /Users/dooz/myproject
> npm run lint -- --fix

> myproject@1.0.0 lint /Users/dooz/myproject
> eslint --ext .jsx,.js src "--fix"

/Users/dooz/myproject/src/bad.js
1:18 error 'arg' is defined but never used no-unused-vars
4:11 error Expected '!==' and instead saw '!=' eqeqeq
4:18 warning Unexpected console statement no-console

✖ 6 problems (4 errors, 2 warnings)

And the bad.js is not too bad anymore because Prettier and ESLint took care of it:

function logTest(arg) {
    const test = ''

    if (test != '') console.log(test)
}

logTest('test')

You just need to fix the other warning manually and you are good.

That’s it. If you have any suggestion or questions, just tweet me.


Daniel Beutner

Personal blog by Daniel Beutner. Dad, coder and musician.