What is standard?
The 'standard' npm package is a JavaScript style guide, linter, and formatter all in one. It enforces a consistent coding style without the need for configuration, making it easier to maintain code quality across projects.
What are standard's main functionalities?
Linting
Linting is the process of running a program that will analyze code for potential errors. The 'standard' package provides a zero-configuration linter that checks for style and programming errors.
npx standard
Auto-fixing
The 'standard' package can automatically fix some of the issues it finds in your code. This feature helps in maintaining code quality by automatically correcting common style and formatting issues.
npx standard --fix
Integration with Editors
The 'standard' package can be integrated with various code editors like VSCode, Sublime Text, and Atom. This allows for real-time linting and auto-fixing as you write code.
/* Example for VSCode */
{
"editor.formatOnSave": true,
"javascript.validate.enable": false,
"standard.enable": true
}
Other packages similar to standard
eslint
ESLint is a highly configurable linter for JavaScript and JSX. Unlike 'standard', which comes with a predefined set of rules, ESLint allows you to define your own rules or extend from popular style guides like Airbnb or Google.
prettier
Prettier is an opinionated code formatter that supports many languages. It focuses on code formatting rather than linting. While 'standard' includes both linting and formatting, Prettier is often used in conjunction with ESLint for a more comprehensive solution.
xo
XO is a JavaScript linter with great defaults and minimal configuration. It is similar to 'standard' in that it aims to provide a zero-config experience, but it also allows for some customization and extends ESLint under the hood.
JavaScript Standard Style
One Style to Rule Them All
No decisions to make, no .jshintrc
or .jscs
files to manage. It just works.
Install
npm install standard
Rules
- 2 spaces for indentation
- Single quotes for strings
- Except to avoid escaping like
"in this lil' string"
- Unix line breaks (LF)
- No semicolons
- Never start a line with
(
or [
:
- This is the one gotcha with omitting semicolons – automatically checked for you!
- Always prefix with
;
like this ;[1, 2, 3].join(' ')
- Spaces after keywords:
- Spaces before/after function definitions:, like this:
function name (arg1, arg2) { ... }
- Always name the context variable
self
:
- Always use
===
instead of ==
- Dozens of sanity checks to catch bugs (unused variables, typos, etc.)
To get a better idea, take a look at
a sample file written
in JavaScript Standard Style.
Usage
The easiest way to use standard
is to install it globally as a Node command line
program. To do so, simply run the following command in your terminal (flag -g
installs
standard
globally on your system, omit it if you want to install in the current working
directory):
npm install standard -g
After you've done that you should be able to use the standard
program. The simplest use
case would be checking the style of all JavaScript files in the current working directory:
$ standard
Error: Code style check failed:
/Users/feross/code/webtorrent/lib/torrent.js:950:11: Expected '===' and instead saw '=='.
The paths node_modules/
, .git/
, *.min.js
, and bundle.js
are automatically excluded
when looking for .js
files to style check.
What you might do if you're clever
- Add it to
package.json
{
"name": "my-cool-package",
"devDependencies": {
"standard": "*"
},
"scripts": {
"test": "standard && node my-normal-tests.js"
}
}
- Check style automatically when you run
npm test
$ npm test
Error: Code style check failed:
/Users/feross/code/webtorrent/lib/torrent.js:950:11: Expected '===' and instead saw '=='.
- Never give style feedback on a pull request again!
License
MIT. Copyright (c) Feross Aboukhadijeh.