JavaScript Standard Style
Standard is a JavaScript style guide, linter, and formatter designed for simplicity and consistency. It enforces a strict, opinionated code style with zero configuration, aiming to eliminate bike-shedding over stylistic choices. Currently at version 17.1.2, it sees active maintenance with minor releases frequently addressing dependency updates and bug fixes, while major versions, like v17.0.0, primarily focus on synchronizing with the broader ESLint ecosystem rather than introducing new rules. Key differentiators include its 'no configuration' philosophy, automatic code formatting via `standard --fix`, and early detection of style issues and common programmer errors, saving time in code reviews. It functions as an all-in-one alternative to separate tools like ESLint and Prettier, using ESLint under the hood but abstracting away its configuration complexity.
Common errors
-
Expected '===' and instead saw '=='.
cause Using the loose equality operator (==) instead of the strict equality operator (===).fixChange `==` to `===` or `!=` to `!==`. -
Extra semicolon.
cause `standard` enforces a 'no semicolons' rule where they are not strictly required.fixRemove the unnecessary semicolon. `standard --fix` can often resolve these automatically. -
Missing space before function parentheses.
cause Function declarations or expressions are missing a space between the function name (or keyword) and its opening parenthesis.fixAdd a space: `function foo ()` instead of `function foo()` or `async () =>` instead of `async() =>`. `standard --fix` often corrects this. -
'someVar' is defined but never used.
cause A variable has been declared but its value is never read or referenced in the code.fixRemove the unused variable or use it. If intentional, you might need to use `// eslint-disable-line no-unused-vars` (but this goes against `standard`'s philosophy). -
'someVar' is never reassigned. Use 'const' instead.
cause A variable declared with `let` or `var` is not reassigned after its initial declaration.fixChange `let` or `var` to `const` for variables that do not change their value. `standard --fix` can automate this.
Warnings
- breaking Version 17.0.0 involved a full synchronization with ESLint 8. This included significant internal dependency updates (e.g., ESLint itself, `eslint-config-standard`, `eslint-plugin-react`), and notably, the replacement of `eslint-config-node` with `eslint-config-n`. This means previous ESLint disable comments like `// eslint-disable-line node/no-deprecated-api` must be updated to reference `n/` rules instead. The `object-shorthand` rule also changed to a warning by default, and `--verbose` output is now standard.
- gotcha `standard` enforces a strict, opinionated style with 'zero configuration'. This means you cannot customize individual rules via a configuration file (e.g., `.eslintrc`). If you require fine-grained control over ESLint rules, you should use ESLint directly with `eslint-config-standard` as a base configuration, which defeats the purpose of using `standard` itself.
- gotcha Using `standard` alongside other formatters like Prettier can lead to conflicts due to differing formatting rules, resulting in inconsistent code and 'formatting wars'. StandardJS has specific opinions (e.g., no semicolons, single quotes) that may clash with Prettier's defaults or other configurations.
- deprecated Running `standard` on Node.js versions older than `^12.22.0 || ^14.17.0 || >=16.0.0` (as specified in `engines` field) might lead to silent failures or unexpected behavior due to the `version-guard` mechanism introduced in v17.1.0. While not an explicit error, it can prevent linting from running correctly without clear feedback.
Install
-
npm install standard -
yarn add standard -
pnpm add standard
Imports
- standard (CLI)
import standard from 'standard'
npx standard --fix
- lintText
const standard = require('standard'); standard.lintText(...)import { lintText } from 'standard' - lintFiles
const standard = require('standard'); standard.lintFiles(...)import { lintFiles } from 'standard'
Quickstart
/* eslint-disable no-unused-vars */
const fs = require('fs')
function exampleFunc(arg1) {;
var unusedVar = 10
const myString = "hello world"
if (arg1 == null) {
console.log(myString)
}
}
// Save this to a file like 'broken.js'
// Then run 'npx standard broken.js --fix'
// You can also add `"test": "standard"` to your package.json scripts and run `npm test`