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.

Answer from Alan Storm on Stack Overflow
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"

🌐
npm
npmjs.com › package › js-beautify
js-beautify - npm
This little beautifier will reformat and re-indent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edward’s popular packer, as well as partly deobfuscate scripts processed by the npm package javascript-obfuscator.
      » npm install js-beautify
    
Published   Feb 27, 2025
Version   1.15.4
Author   Einar Lielmanis
🌐
Beautifier
beautifier.io
Online JavaScript beautifier
Chrome, in case the built-in CSS and javascript formatting isn't enough for you: — Quick source viewer by Tomi Mickelsson (github, blog), — Javascript and CSS Code beautifier by c7sky, — jsbeautify-for-chrome by Tom Rix (github), — Pretty Beautiful JavaScript by Will McSweeney — Stackoverflow Code Beautify by Making Odd Edit Studios (github).
🌐
JSON Formatter
jsonformatter.org › jsbeautifier
Best JSBeautifier to beautify / format JavaScript
Best and Secure Online Javascript Formatter and Javascript Beautifier works well in Windows, Mac, Linux, Chrome, Firefox, Safari, and Edge.
🌐
GeeksforGeeks
geeksforgeeks.org › utilities › javascript-formatter
Online Free JavaScript Formatter and Beautifier - GeeksforGeeks
November 10, 2023 - Linux · DevOps · Last Updated : 2 Mar, 2026 · This free Online Javascript Formatter/Beautifier allows you to edit, view, analyze, beautify, and format JavaScript code. To use this online JS beautifier, follow the given steps: Step 1. Input the JavaScript code in the first box.
🌐
Prettier
prettier.io
Prettier · Opinionated Code Formatter · Prettier
Your code is formatted on save · No need to discuss style in code review · Saves you time and energy · And more » · JavaScript · JSX · Flow · TypeScript · JSON · CSS · Less · SCSS · styled-components 💅 · styled-jsx · HTML · Vue · Angular · Ember / Handlebars ·
🌐
Code Beautify
codebeautify.org › jsviewer
Best Javascript Beautifier tool work as JavaScript Formatter, Viewer and Prettier
It helps to run your Javascript. Beautify JS Online works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. ... JS data Try it. var carInsuranceCompany = { name: "Geico", market_capital: "$34.9 billion", }; var carInsuranceCompanyObj ...
🌐
Kali Linux Tools
en.kali.tools
JS Beautifier - Penetration Testing Tools
This little beautifier will reformat and re-indent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edward’s popular packer, as well as partly deobfuscate scripts processed by the npm package javascript-obfuscator.
Find elsewhere
🌐
Arch Linux
aur.archlinux.org › packages › js-beautify
AUR (en) - js-beautify - Arch Linux
1&nbsp2&nbsp3&nbsp4 Next › Last » · The chown is no longer necessary
🌐
GitHub
github.com › yasuyk › web-beautify
GitHub - yasuyk/web-beautify: Format HTML, CSS and JavaScript/JSON by js-beautify
It uses the command-line/node.js javascript formatter from http://jsbeautifier.org/ to format whole html, css, js or json files, or region.
Starred by 223 users
Forked by 21 users
Languages   Emacs Lisp 82.2% | Makefile 17.8%
🌐
Minifier
minifier.org › javascript-beautifier
Best Online JavaScript Beautifier / Formatter
Just paste your JavaScript code into the editor and click the Beautify button. Your formatted code will appear immediately. Our tool is compatible with Windows, Mac, and Linux, and works on all modern browsers including Chrome, Firefox, Safari, and Edge.
🌐
GitHub
github.com › beautifier › js-beautify
GitHub - beautifier/js-beautify: Beautifier for javascript · GitHub
This little beautifier will reformat and re-indent bookmarklets, ugly JavaScript, unpack scripts packed by Dean Edward’s popular packer, as well as partly deobfuscate scripts processed by the npm package javascript-obfuscator.
Author   beautifier
🌐
Format.JS
formatjs.github.io
FormatJS - Internationalize your web apps on the client & server
Industry-standard i18n libraries for JavaScript. FormatJS provides modular libraries for formatting numbers, dates, and strings for React, Vue, and vanilla JS. Built on ICU Message syntax and ECMA-402.
🌐
Package Control
packagecontrol.io › packages › JsFormat
JsFormat - Packages - Package Control
Open the command palette via “ctrl + shift + p”, Jsformat appears as “Format: Javascript”
🌐
GitHub
github.com › prettier › prettier
GitHub - prettier/prettier: Prettier is an opinionated code formatter. · GitHub
JavaScript · TypeScript · Flow · JSX · JSON CSS · SCSS · Less HTML · Vue · Angular GraphQL · Markdown · YAML Your favorite language? Prettier is an opinionated code formatter.
Author   prettier
🌐
Package Control
packagecontrol.io › packages › HTML-CSS-JS Prettify
HTML-CSS-JS Prettify - Packages - Package Control
This is a Sublime Text 2 and 3 plugin allowing you to format your HTML, CSS, JavaScript, JSON, React and Vue code. It uses a set of nice beautifier scripts made by Einar Lielmanis. The formatters are written in JavaScript, so you'll need something (node.js) to interpret JavaScript code outside ...
🌐
Prepostseo
prepostseo.com › js-beautifier
JS Beautifier - Unminify Javascript with JS formatter/Cleaner
Meet Javascript Beautifier, a simple online tool that does exactly what it says; beautify JS quickly and easily. Prettify your JS in a hassle-free way, or beautify it into human-readable format.