Skip to main content

Synopsis

js-beautify [options] <file ...>
When installed globally (npm install -g js-beautify), the js-beautify executable is available on your PATH. Beautified output is written to stdout by default.
Pass - as a filename to read from stdin. When no files are provided and no stdin pipe is detected, the CLI prints an error and usage information.

Options

I/O options

FlagLong formDescription
-f--fileInput file(s). Pass - for stdin.
-r--replaceWrite output in-place, overwriting the input file.
-o--outfileWrite output to a specific file instead of stdout.
--configPath to a JSON config file.
--typeSelect beautifier type: js, css, or html. Does not filter input files — only selects which beautifier to run. Default: js.
-q--quietSuppress logging to stdout.
-h--helpShow help and exit.
-v--versionShow the version number and exit.

Indentation options

FlagLong formDefaultDescription
-s--indent-size4Number of spaces per indentation level.
-c--indent-char" "Character used for indentation.
-t--indent-with-tabsfalseIndent with tabs, overrides -s and -c.
-l--indent-level0Initial indentation level.
--indent-empty-linesfalseKeep indentation on empty lines.
--editorconfigfalseUse EditorConfig to set up options from .editorconfig.

Output formatting

FlagLong formDefaultDescription
-e--eolfirst newline in file, else \nLine terminator character(s).
-n--end-with-newlinefalseEnd output with a newline.
-p--preserve-newlinestruePreserve existing line breaks. Use --no-preserve-newlines to disable.
-m--max-preserve-newlines10Maximum blank lines to preserve in one chunk.
-w--wrap-line-length0Wrap lines exceeding N characters. 0 disables wrapping.
-b--brace-stylecollapseBrace style: collapse, expand, end-expand, none. Append ,preserve-inline to keep single-line blocks.
-C--comma-firstfalsePlace commas at the start of new lines instead of the end.
-O--operator-positionbefore-newlineOperator position on wrapped lines: before-newline, after-newline, preserve-newline.
--templatingautoTemplating languages to recognize: auto, none, django, erb, handlebars, php, smarty, angular.

Space options

FlagLong formDefaultDescription
-P--space-in-parenfalseAdd padding spaces inside parentheses: f( a, b ).
-Q--space-in-empty-parenfalseAdd a space inside empty parentheses: f( ).
-j--jslint-happyfalseEnable JSLint-stricter mode. Implies --space-after-anon-function.
-a--space-after-anon-functionfalseAdd a space before anonymous function parens: function ().
--space-after-named-functionfalseAdd a space before named function parens: function example ().

Method chaining

FlagLong formDefaultDescription
-u--unindent-chained-methodsfalseDo not indent chained method calls.
-B--break-chained-methodsfalseBreak chained method calls onto separate lines.

Miscellaneous

FlagLong formDefaultDescription
-k--keep-array-indentationfalsePreserve original array indentation.
-x--unescape-stringsfalseDecode printable characters encoded in \xNN notation.
-X--e4xfalsePass E4X XML literals through untouched.
--good-stuffEnables --keep-array-indentation and --jslint-happy together.

Examples

# Beautify from stdin to stdout (pipe to - filename)
echo 'function foo(){return 42;}' | js-beautify -

# Beautify a file, output to stdout
js-beautify app.js

# Beautify multiple files to stdout
js-beautify src/*.js

# Replace files in-place
js-beautify --replace src/main.js src/utils.js

# Write beautified output to a new file
js-beautify -o dist/formatted.js src/main.js

# Use 2-space indentation and end with a newline
js-beautify -s 2 -n app.js

# Indent with tabs, expand braces
js-beautify --indent-with-tabs --brace-style expand app.js

# Use a config file
js-beautify --config .jsbeautifyrc app.js

# Pipe from another tool
cat bundle.min.js | js-beautify - -s 2 > bundle.formatted.js
You can also use js-beautify --css or js-beautify --html as an alternative to the css-beautify and html-beautify commands. The --type flag selects the beautifier without filtering input files.

Configuration loading

The CLI resolves options from the following sources in priority order (highest to lowest):
  1. CLI flags passed directly
  2. jsbeautify_-prefixed environment variables
  3. A JSON config file specified via --config
  4. A .jsbeautifyrc JSON file found at or above the current working directory
Option names in .jsbeautifyrc use underscores instead of dashes, matching the library API names.