Search code examples
javascripteslintprettier

How to get ESLint --fix to also run prettier --write


It is possible to configure your .eslintrc so that running eslint --fix would run also run prettier --write as well?

Right now I need to do prettier --write && eslint --fix and I was hoping I could just run the one command.

Update

Using prettier-eslint "works" except it does not apply my custom overrides:

My .eslintrc.js looks like

module.exports = { 
  extends: [
    'prettier',
  ],
  plugins: [
    'prettier',
  ],
  rules: {
    'prettier/prettier': [
      'error',
      {
        'singleQuote': true,
      },
    ],
  }
};

Then I have a dummy file.js:

const path = require("path");
const foo = () => {
    return path;
}

If I run eslint ./file.js I get expected output:

error Replace "path" with 'path' prettier/prettier

But when I run prettier-eslint --eslint-config-path .eslintrc.js --write ./file.js it keeps the " and does not replace it with '

I do not want to have a separate file for prettierrc and want to keep both in one file.


Solution

  • There's a great package called prettier-eslint that we've used at work before. It will format using prettier and then run eslint --fix on your code. We liked it, give it a try!