Installing Lint-Staged
What you'll learn: How to add lint-staged to your project using npm or yarn and set up its configuration.
Now that you know what lint-staged is (a tool that runs linters only on staged Git files), let's get it into your project. The installation is straightforward, but you have choices about where to configure it.
Installing with npm or yarn
Install lint-staged as a development dependency:
npm install --save-dev lint-staged
Or with yarn:
yarn add --dev lint-staged
Since lint-staged is a build-time tool (not needed in production), it belongs in devDependencies.
Configuring lint-staged
You have two main options for configuration:
Option 1: Inside package.json
Add a lint-staged field directly to your package.json:
{
"devDependencies": {
"lint-staged": "^15.0.0"
},
"lint-staged": {
"*.js": "eslint --fix"
}
}
This approach keeps everything in one file—simple for smaller projects.
Option 2: Dedicated config file
Create a .lintstagedrc.json file in your project root:
{
"*.js": "eslint --fix"
}
You can also use .lintstagedrc.js for more complex JavaScript-based configuration, but the JSON approach is cleaner for most cases.
Which to choose?
Use package.json if you want fewer files. Use a dedicated config file if your lint-staged setup is complex or you prefer separation of concerns. Both work identically—it's purely organizational.
Key Takeaway: Install lint-staged with npm install --save-dev lint-staged and configure it either in your package.json under a "lint-staged" field or in a dedicated .lintstagedrc.json file—both approaches give you the same functionality.