Stack Exchange is using an old version of js-beautify to beautify JS, HTML, and CSS. Dynamic import isn't the only thing the old version gets wrong. It also breaks code when the code contains any of the following:

  • Numeric separators (fixed in new version):

    1_000_000
    

    turns into

    1 _000_000
    
  • Private fields (fixed in new version):

    class X { #foo = 'bar'; }
    

    turns into

    class X {#
      foo = 'bar';
    }
    
  • Logical assignment (fixed in new version):

    a ||= b;
    

    turns into

    a || = b;
    
  • Nullish coalescing (fixed in new version):

    x ?? y
    

    turns into

    x ? ? y
    
  • Optional chaining (fixed in new version):

    x?.y?.z
    

    turns into

    x ? .y ? .z
    

The new version of js-beautify also supports dynamic import:

const code = `
await import('./module.js');   // GOOD

1_000_000                      // GOOD

class X { #foo = 'bar'; }      // GOOD

a ||= b;                       // GOOD

x ?? y                         // GOOD

x?.y?.z                        // GOOD
`;

console.log(js_beautify(code));
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.6/beautify.min.js"></script>

Let's update js-beautify.

Answer from CertainPerformance on Stack Overflow
๐ŸŒ
Beautifier
beautifier.io
Online JavaScript beautifier
Beautify JavaScript, JSON, React.js, HTML, CSS, SCSS, and SASS
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ jsbeautifier
Best JSBeautifier to beautify / format JavaScript
Online JS Beautifier helps to Beautify Javascript. This Javascript Beautifier helps to unminify, Save and Share Javascript.
๐ŸŒ
Utilities and Tools
utilities-online.info โ€บ js-beautifier
JS Beautifier
Beautify JavaScript using JS beautifier tool. This JavaScript Beautifier will format your JS code at one click.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ js-beautify
js-beautify - npm
February 27, 2025 - beautifier.io for node. Latest version: 1.15.4, last published: a year ago. Start using js-beautify in your project by running `npm i js-beautify`. There are 3131 other projects in the npm registry using js-beautify.
      ยป npm install js-beautify
    
Published ย  Feb 27, 2025
Version ย  1.15.4
Author ย  Einar Lielmanis
Homepage ย  https://beautifier.io/
๐ŸŒ
GitHub
github.com โ€บ beautifier โ€บ js-beautify
JS Beautifier
Open beautifier.io. Options are available via the UI. After you embed the <script> tags in your html file, they expose three functions: js_beautify, css_beautify, and html_beautify
Author ย  beautifier
๐ŸŒ
Code Beautify
codebeautify.org โ€บ jsviewer
Best Javascript Beautifier tool work as JavaScript Formatter, Viewer and Prettier
Secure JavaScript Beautifier, Viewer, Editor, Minify, Formatter, Obfuscator - Convert JS/Javascript Strings to a Friendly Readable Format.
Find elsewhere
๐ŸŒ
JSONLint
jsonlint.com
JSONLint - The JSON Validator
JSONLint is the free online validator, json formatter, and json beautifier tool for JSON, a lightweight data-interchange format.
๐ŸŒ
EasyRetro
easyretro.io โ€บ tools โ€บ js-beautifier
JS Beautifier (Simple and Easy) | EasyRetro
JS Beautify is a technique used to help developers format code in a consistent way. Beautify helps to ensure all their code is up to the latest formatting standards.
๐ŸŒ
FreeFormatter
freeformatter.com โ€บ javascript-beautifier.html
Free Online Javascript Beautifier / Formatter - FreeFormatter.com
This free online tool lets you beautify/format your JavaScript code with no side effects.
๐ŸŒ
CodeSandbox
codesandbox.io โ€บ s โ€บ beautify-webjs-beautify-tplx1
beautify-web/js-beautify - CodeSandbox
October 11, 2020 - beautify-web/js-beautify using config-chain, editorconfig, glob, mkdirp, nopt
Published ย  Oct 11, 2020
Top answer
1 of 10
76

First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it's what I found first. Downloads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js

Second, download and install The Mozilla group's Java based Javascript engine, Rhino. "Install" is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or Library/Java/Extensions on OS X). You can then run scripts with an invocation similar to this

java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js

Use the Pretty Print/Beautifier from step 1 to write a small shell script that will read in your javascript file and run it through the Pretty Print/Beautifier from step one. For example

//original code    
(function() { ... js_beautify code ... }());

//new code
print(global.js_beautify(readFile(arguments[0])));

Rhino gives javascript a few extra useful functions that don't necessarily make sense in a browser context, but do in a console context. The function print does what you'd expect, and prints out a string. The function readFile accepts a file path string as an argument and returns the contents of that file.

You'd invoke the above something like

java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js

You can mix and match Java and Javascript in your Rhino run scripts, so if you know a little Java it shouldn't be too hard to get this running with text-streams as well.

2 of 10
64

UPDATE April 2014:

The beautifier has been rewritten since I answered this in 2010. There is now a python module in there, an npm Package for nodejs, and the jar file is gone. Please read the project page on github.com.

Python style:

$ pip install jsbeautifier

NPM style:

$ npm -g install js-beautify

to use it (this will return the beatified js file on the terminal, the main file remains unchanged):

$ js-beautify file.js

To make the changes take effect on the file, you should use this command:

$ js-beautify -r file.js

Original answer

Adding to Answer of @Alan Storm

the command line beautifier based on http://jsbeautifier.org/ has gotten a bit easier to use, because it is now (alternatively) based on the V8 javascript engine (c++ code) instead of rhino (java-based JS engine, packaged as "js.jar"). So you can use V8 instead of rhino.

How to use:

download jsbeautifier.org zip file from http://github.com/einars/js-beautify/zipball/master

(this is a download URL linked to a zip file such as http://download.github.com/einars-js-beautify-10384df.zip)

old (no longer works, jar file is gone)

java -jar js.jar  name-of-script.js

new (alternative)

install/compile v8 lib FROM svn, see v8/README.txt in above-mentioned zip file

./jsbeautify somefile.js

-has slightly different command line options than the rhino version,

-and works great in Eclipse when configured as an "External Tool"

๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ javascript โ€บ javascript formatter
Online JavaScript Formatter and Beautifier
Online JavaScript (JS) Formatter - Transform messy JavaScript (JS) code into clean, readable code with this JavaScript Beautifier tool, saving time and effortlessly improving code quality.
๐ŸŒ
Toolvat
toolvat.com โ€บ js-beautifier
JS Beautifier - Toolvat
Session doesn't seem to be active ยท Read the CodeIgniter docs
๐ŸŒ
BeautifyTools
beautifytools.com โ€บ javascript-beautifier.php
Online Javascript Beautifier - Javascript Formatter - BeautifyTools.com
Online Javascript beautifier takes ugly, minified or obfuscated javascript and make it clean, well-formatted code. It gives the code proper indentation, newlines, spaces to make it easier to read.
๐ŸŒ
Minifier
minifier.org
Minify JS and CSS online, or include the minifier in your project for on-the-fly compression.
Minify JS and CSS online using this javascript & CSS compressor , or include the minifier in your project.
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
JS Beautify - Visual Studio Marketplace
March 15, 2024 - Extension for Visual Studio Code - A little wrapper around "js-beautify" for conveniently beautifying CSS/HTML/JS files.
๐ŸŒ
GitHub
github.com โ€บ beautifier โ€บ js-beautify โ€บ issues
beautifier/js-beautify
Beautifier for javascript . Contribute to beautifier/js-beautify development by creating an account on GitHub.
Author ย  beautifier
๐ŸŒ
Dev Utils
dev-utils.org โ€บ tools โ€บ js-beautifier
JS Beautifier | Dev Utils | A Collection of tools for Developers
December 22, 2025 - Fast, reliable tools for developers: encoders, converters, beautifiers, generators, and more.