Documentation Index
Fetch the complete documentation index at: https://mintlify.com/beautifier/js-beautify/llms.txt
Use this file to discover all available pages before exploring further.
Installation
The Python jsbeautifier package only reformats JavaScript. It does not support HTML or CSS. For CSS, install the separate cssbeautifier package.
Module
Functions
beautify()
Beautify a JavaScript source string.
jsbeautifier.beautify(source: str, opts=None) -> str
The JavaScript source code string to beautify.
An options object returned by default_options(). If omitted, default options are used.
The beautified JavaScript source code.
beautify_file()
Read a JavaScript file and return beautified output as a string.
jsbeautifier.beautify_file(file_name: str, opts=None) -> str
Path to the JavaScript file. Pass "-" to read from stdin.
An options object returned by default_options().
The beautified JavaScript source code.
default_options()
Return a BeautifierOptions instance populated with default values.
jsbeautifier.default_options() -> BeautifierOptions
A BeautifierOptions object. Mutate its attributes before passing to beautify() or beautify_file().
BeautifierOptions attributes
All attributes mirror the CLI flag names with underscores instead of dashes.
Number of spaces per indentation level.
Character used for indentation.
Indent with tabs instead of spaces.
Line terminator character(s). auto uses the first newline found in the input, or \n if none is found.
Ensure the output ends with a newline.
Initial indentation level.
Preserve existing blank lines.
Maximum consecutive blank lines to preserve.
Add padding spaces inside parentheses: f( a, b ).
Add a single space inside empty parentheses: f( ).
Enable JSLint-stricter mode. Implies space_after_anon_function = True.
space_after_anon_function
Add a space before anonymous function parens: function ().
space_after_named_function
Add a space before named function parens: function example ().
Brace placement style: collapse, expand, end-expand, none. Append ,preserve-inline to keep single-line blocks.
Do not indent chained method calls.
Break chained method calls onto separate lines.
Preserve original array indentation.
Decode printable characters encoded in \xNN notation.
Wrap lines exceeding this many characters. 0 disables wrapping.
Pass E4X XML literals through untouched.
Place commas at the start of new lines instead of the end.
operator_position
str
default:"before-newline"
Operator position on wrapped lines: before-newline, after-newline, preserve-newline.
Keep indentation whitespace on otherwise empty lines.
Templating languages to recognize. auto disables all in JavaScript context.
Evaluate code using a JS interpreter before beautifying. Potential security risk.
Add a space before conditional expressions such as if (.
Examples
import jsbeautifier
# Beautify with default options
result = jsbeautifier.beautify('function hello(name){return "Hello, "+name;}')
print(result)
# function hello(name) {
# return "Hello, " + name;
# }
# Beautify with custom options
opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True
opts.end_with_newline = True
result = jsbeautifier.beautify('function foo(){var x=1;return x;}', opts)
print(result)
# function foo() {
# var x = 1;
# return x;
# }
# Beautify a file
result = jsbeautifier.beautify_file('app.min.js')
with open('app.js', 'w') as f:
f.write(result)
# Read from stdin
import sys
result = jsbeautifier.beautify_file('-') # reads from stdin
sys.stdout.write(result)
CLI (Python)
The jsbeautifier package also installs a js-beautify command:
# Beautify a file
js-beautify app.min.js
# Specify options
js-beautify -s 2 --end-with-newline app.min.js
# Replace in-place
js-beautify -r app.min.js
# Write to a specific output file
js-beautify -o app.js app.min.js
Run js-beautify --help for a full list of CLI flags.