Registry / serialization / jsbeautifier

jsbeautifier

JSON →
library2.0.3pypypi✓ verified 25d ago

jsbeautifier is the official Python wrapper for the popular js-beautify project, providing JavaScript, HTML, and CSS code formatting and beautification capabilities. It is actively maintained with releases typically aligning with the core JavaScript library, currently at version 1.15.4.

pip install jsbeautifier
INSTALL
IMPORT
SIG · JSBEAUTIFIER
J
jsbeautifier
serializationpythonv2.0.3
Install
1.7s avg
Import
37ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 18.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.034s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

beautify
from jsbeautifier import beautify
html_beautify
from jsbeautifier import html_beautify
css_beautify
from jsbeautifier import css_beautify
default_options
from jsbeautifier import default_options

This quickstart demonstrates how to beautify JavaScript and HTML code using `jsbeautifier`. It shows how to import the necessary functions, create and customize an options object, and apply it to format code strings.

import jsbeautifier # Example for JavaScript beautification js_code = "function test( ){ var x=1;if(x){ return x;} else { return 0;}}" # Create and customize options object options = jsbeautifier.default_options() options.indent_size = 2 options.space_in_paren = True options.keep_array_indentation = True beautified_js = jsbeautifier.beautify(js_code, options) print("Original JS:\n", js_code) print("\nBeautified JS:\n", beautified_js) # Example for HTML beautification (using the same options) html_code = "<html><head><title>Test</title></head><body><h1>Hello</h1> <p>World</p></body></html>" beautified_html = jsbeautifier.html_beautify(html_code, options) print("\nOriginal HTML:\n", html_code) print("\nBeautified HTML:\n", beautified_html)
js-beautify --version
Debug
Known issues
gotchaThe PyPI metadata for `jsbeautifier` incorrectly states `requires_python: None`. The actual minimum Python version required by `jsbeautifier` 1.15.4 and newer is Python 3.7.
fix
Ensure your environment uses Python 3.7 or higher. Always consult the `python_requires` metadata in the official source distribution or `pyproject.toml` for the most accurate requirement.
affects: 1.15.4+
gotchaBeautifier options must be passed as an `options` object, typically initialized via `jsbeautifier.default_options()`, rather than directly as keyword arguments to `beautify()`, `html_beautify()`, or `css_beautify()`.
fix
Create an options object: `opts = jsbeautifier.default_options(); opts.indent_size = 4; beautified = jsbeautifier.beautify(code, opts)`.
affects: All versions
gotchaThe `jsbeautifier` Python package's version directly reflects the version of the underlying `js-beautify` JavaScript project. Breaking changes in the core `js-beautify` library will implicitly affect the Python wrapper, even if the Python API surface itself doesn't change.
fix
Always consult the changelog of the main `beautify-web/js-beautify` GitHub repository for potential behavioral changes or deprecations when upgrading `jsbeautifier`.
affects: All versions
gotchaThe `jsbeautifier` Python package is specifically designed to wrap the JavaScript beautifier (`js-beautify`). It does not include or expose `html_beautify` or `css_beautify` functions, which are distinct components of the broader upstream `js-beautify` project.
fix
Only `jsbeautifier.beautify()` is available for JavaScript code beautification. For HTML or CSS beautification, consider using alternative Python libraries or tools, as these functionalities are not provided by the `jsbeautifier` package.
affects: All versions
breakingThe `jsbeautifier.html_beautify()` and `jsbeautifier.css_beautify()` functions were renamed to `jsbeautifier.beautify_html()` and `jsbeautifier.beautify_css()` respectively.
fix
Replace calls to `jsbeautifier.html_beautify()` with `jsbeautifier.beautify_html()` and `jsbeautifier.css_beautify()` with `jsbeautifier.beautify_css()`.
affects: 1.15.0+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'jsbeautifier.javascript'
This error typically occurs when the `jsbeautifier` package or its submodules are not correctly installed, or there's an issue with the package's internal structure during installation or import. It can also arise when a dependent package attempts to import a submodule of `jsbeautifier` that doesn't exist or isn't accessible.
fix
Ensure `jsbeautifier` is properly installed and up-to-date by running `pip install --upgrade jsbeautifier`. If the error persists, especially when installing other packages like `cssbeautifier`, try installing `jsbeautifier` first before the dependent package.
'charmap' codec can't decode byte 0x... in position X: character maps to <undefined>
This `UnicodeDecodeError` or a similar 'charmap' codec error happens when `jsbeautifier` tries to read a file or string containing non-ASCII characters (e.g., special symbols, international text) using an incompatible default encoding (like 'charmap' on Windows or a default 'utf-8' without proper handling), while the actual data is in a different encoding.
fix
Explicitly specify the correct encoding, usually 'utf-8', when reading the input content or opening files.
```python
import jsbeautifier

# When reading from a file
with open('input.js', 'r', encoding='utf-8') as f:
    input_code = f.read()
beautified_code = jsbeautifier.beautify(input_code)

# When writing to a file, also specify encoding
with open('output.js', 'w', encoding='utf-8') as f:
    f.write(beautified_code)
```
option --config not recognized
This error occurs when using the `js-beautify` command-line tool (from the Python `jsbeautifier` package) with options like `--config` that are supported by the original JavaScript `js-beautify` CLI but are not implemented in the Python wrapper's command-line interface. The Python wrapper's CLI might have a more limited set of direct command-line arguments.
fix
Instead of relying on command-line flags, use the programmatic interface of `jsbeautifier` in Python to pass options. You can load configuration from a file manually and then apply it.
```python
import jsbeautifier
import json

# Define options programmatically or load from a file
opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True

# Example: If you had a .jsbeautifyrc file
# with open('.jsbeautifyrc', 'r') as f:
#     config_from_file = json.load(f)
# for key, value in config_from_file.items():
#     setattr(opts, key, value)

code = "function test(){var x=1;}"
beautified_code = jsbeautifier.beautify(code, opts)
print(beautified_code)
```
Upgrade
Version history
2.0.3latest on PyPI · released Jun 30, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
Resources