LinuxCommandLibrary

js-beautify

TLDR

Beautify JavaScript file

$ js-beautify [file.js]
copy
Beautify and save in place
$ js-beautify -r [file.js]
copy
Beautify HTML
$ html-beautify [file.html]
copy
Beautify CSS
$ css-beautify [file.css]
copy
Set indentation
$ js-beautify -s [4] [file.js]
copy
Use tabs
$ js-beautify -t [file.js]
copy
Output to file
$ js-beautify [input.js] -o [output.js]
copy

SYNOPSIS

js-beautify [options] [file...]

DESCRIPTION

js-beautify formats JavaScript, HTML, and CSS files with consistent indentation and style. It's useful for cleaning up minified code or enforcing consistent formatting.
The tool provides separate commands for each language (js-beautify, html-beautify, css-beautify) or auto-detects based on extension.

PARAMETERS

-r, --replace

Replace file in place.
-o file
Output to file.
-s size
Indent size (default 4).
-t, --indent-with-tabs
Use tabs.
-e, --eol chars
End of line character.
-n, --end-with-newline
End with newline.
--type type
Force type: js, html, css.
-f file
Input file.
--config file
Configuration file.

CONFIGURATION

$ // .jsbeautifyrc
{
  "indent_size": 2,
  "indent_with_tabs": false,
  "end_with_newline": true,
  "brace_style": "collapse"
}
copy

CAVEATS

May alter code semantics in edge cases. Large files may be slow. Doesn't validate code. Configuration precedence can be complex.

HISTORY

js-beautify was created by Einar Lielmanis as an online tool, later expanded to a command-line tool and library. It's widely used for code formatting and reversing minification.

SEE ALSO

prettier(1), eslint(1), uglifyjs(1)

Copied to clipboard