Skip to main content

Installation

Install the package globally to get the CLI executables on your PATH:
npm install --global js-beautify

Executables

Installing js-beautify globally provides three executables:
ExecutablePurpose
js-beautifyBeautify JavaScript (also accepts --type css or --type html)
css-beautifyBeautify CSS
html-beautifyBeautify HTML

Basic Usage

Pass a file path as the argument. Beautified output is written to stdout by default.
js-beautify foo.js
css-beautify styles.css
html-beautify index.html

Flags

Input and Output

FlagDescription
-f, --fileInput file (pass - to read from stdin)
-o, --outfileWrite output to a file instead of stdout
-r, --replaceOverwrite the input file in-place
--type [js|css|html]Select which beautifier to run (default: js)
--configPath to a JSON config file
-q, --quietSuppress logging to stdout

Formatting

FlagDefaultDescription
-s, --indent-size4Number of spaces per indentation level
-c, --indent-char" "Character to use for indentation
-t, --indent-with-tabsIndent with tabs (overrides -s and -c)
-e, --eolautoLine terminator character(s). auto uses first newline in file, else \n.
-n, --end-with-newlineEnd the output with a newline
-b, --brace-stylecollapseBrace placement (collapse|expand|end-expand|none)
-w, --wrap-line-length0Wrap lines exceeding N characters
-p, --preserve-newlinesPreserve existing line breaks
-m, --max-preserve-newlines10Max blank lines to preserve in one chunk

Examples

Write output to a new file

js-beautify foo.js -o foo.pretty.js

Overwrite a file in-place

js-beautify -r foo.js
The -r / --replace flag overwrites the original file with no confirmation. Make sure your file is tracked in version control before using it.

Read from stdin (pipe)

Pass - as the filename to read from stdin:
cat foo.js | js-beautify -
echo 'function x(){return 1;}' | js-beautify -f - -s 2

Set indent size

js-beautify -s 2 foo.js
css-beautify -s 2 styles.css

Use a config file

js-beautify --config .jsbeautifyrc foo.js

Force a specific beautifier type

js-beautify defaults to JavaScript. Use --type to run a different beautifier:
js-beautify --type css styles.css
js-beautify --type html index.html

npm Scripts Integration

You can integrate js-beautify into your build process via package.json scripts. Because npm adds local node_modules/.bin to PATH, you don’t need a global install.
{
  "scripts": {
    "format": "js-beautify -r src/**/*.js",
    "format:css": "css-beautify -r src/**/*.css",
    "format:html": "html-beautify -r src/**/*.html"
  }
}
npm run format

Configuration Sources

You can configure the CLI without passing flags every time. The JS executable loads settings from the following sources, in order of precedence (earlier sources override later ones):
  1. Any jsbeautify_-prefixed environment variables
  2. A JSON file passed via --config
  3. A .jsbeautifyrc file found at or above the current working directory