API Reference

Stylelint

Modern linter for CSS and CSS-like syntaxes that catches errors and enforces conventions through a Node API.

Official Documentation ↗
CompatibilityThis documentation covers version v16. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev stylelint stylelint-config-standard
pnpmpnpm add -D stylelint stylelint-config-standard

Methods

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.

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.