Installing and Configuring Stylelint
You'll learn how to add Stylelint to your project, create its configuration file, and choose the right setup format for your needs.
Getting Stylelint Into Your Project
Just like you installed ESLint with npm, Stylelint follows the same pattern. You'll typically install it as a development dependency:
npm install --save-dev stylelint stylelint-config-standard
The stylelint package is the core linter, while stylelint-config-standard provides a sensible set of default rules (similar to how ESLint has shareable configs).
Creating Your Configuration File
Stylelint looks for a configuration file in your project root. You have several format options:
.stylelintrc.json (JSON format):
{
"extends": "stylelint-config-standard",
"rules": {}
}
stylelint.config.js (JavaScript format):
module.exports = {
extends: "stylelint-config-standard",
rules: {}
};
Or in package.json:
{
"stylelint": {
"extends": "stylelint-config-standard"
}
}
Choosing Your Configuration Format
Think of configuration formats like choosing between a pre-printed form (JSON) and a blank notebook (JavaScript). JSON is simpler and more portable, while JavaScript lets you use variables, logic, and comments. For most projects starting out, .stylelintrc.json is perfectly adequate and keeps things straightforward.
The extends property works just like in ESLint—it pulls in a pre-configured set of rules so you don't start from scratch.
Key Takeaway: Installing Stylelint requires both the core package and a config preset via npm, followed by creating a configuration file that typically extends a standard ruleset—choose JSON for simplicity or JavaScript for flexibility.