NPM — How to Run ESLint --fix From npm Script

NPM is a powerful package manager. It supports a scripts property in the package.json file. These scripts allow you to run commands for the current project. A typical command is to run the test suite for the project.

You can also combine scripts or add CLI flags to individual commands. This tutorial shows you how to run ESLint with the --fix flag from an NPM script.

NPM Quick Tips Series Overview

Run ESLint --fix From npm Script

You can pass along CLI flags to your npm commands. You must add two dashes after your command to run an npm script with a CLI flag.

You can’t run npm run <script> --flag.

The correct way to pass along CLI flags is this:

npm run lint -- --fix  

In situations where you’re starting an npm script from within another npm script, you must also add the two dashes before passing along the CLI flag.

In reference to the ESLint fix command, you may lint your code before running your tests. For example, your test script may then contain calls to run the tests and afterward the test suite.

You may already have a lint script. You can then create a lint:fix script reusing the lint script passing along the --fix flag. In combination, ESLint knows what to lint and also to fix the code.

Here’s a sample setup showing you how to run npm scripts while passing along CLI flags:

package.json

{
  "name": "your-package",
  "description": "…",
  "version": "1.0.0",
  …
  "scripts": {
    "lint": "eslint src --ext .ts",
    "lint:fix": "npm run lint -- --fix",
    "test": "run lint && npm run test:run",
    "test:run": "jest"
  }
}

That’s it! Enjoy npm scripts!

Explore the Library

Find interesting tutorials and solutions for your problems.