Stylelint
Modern linter for CSS and CSS-like syntaxes that catches errors and enforces conventions through a Node API.
Official Documentation ↗v16. Check the official docs for the latest API surface.Installation
npm install --save-dev stylelint stylelint-config-standardpnpm add -D stylelint stylelint-config-standardMethods
No methods match this filter.
Overview
Stylelint is a CSS linter that detects errors and enforces conventions, with a Node API supporting strings, files, fixes, and custom plugins. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
stylelint.lint— Lints CSS from a string or files and resolves with warnings, errors, and result metadata.lint with fix— Applies autofixable rules and writes corrected CSS back to the files on disk.stylelint.createPlugin— Registers a custom rule so projects can enforce their own CSS conventions.
Example
const stylelint = require('stylelint');
const result = await stylelint.lint({
code: 'a { color: #FFF; }',
config: { rules: { 'color-hex-length': 'short' } },
});
console.log(result.results[0].warnings);
Notes
Non-standard syntaxes such as SCSS require a customSyntax. Fixes are only written to disk when linting files rather than a code string.
Verification
Lint a rule-breaking snippet with fix enabled and confirm the returned output differs and warnings drop on a second pass.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://stylelint.io/user-guide/usage/node-api · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:42.353Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- When the signal is aborted, stylelint.lint() will reject with the signal’s reason. (https://stylelint.io/user-guide/usage/node-api)
- Stylelint throws a configuration error for any other option. (https://stylelint.io/user-guide/usage/node-api)
- If true , at least one rule with an “error”-level severity registered a problem. (https://stylelint.io/user-guide/usage/node-api)
FAQ
- How do I lint SCSS or other syntaxes?
- Set customSyntax to postcss-scss or a stylelint-compatible parser in the config for non-standard CSS syntaxes.
- Why are fixes not written with the lint API?
- Pass fix: true to stylelint.lint with files; when linting a code string the fixed output is returned instead of written to disk.
- How do I share a config across packages?
- Publish an npm package that exports a config object and reference it via the extends field in each project config.
Report a problem with this API docOpens a pre-filled issue on GitHub.