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"

🌐
GitHub
github.com › beautifier › js-beautify
GitHub - beautifier/js-beautify: Beautifier for javascript · GitHub
CLI Options: -f, --file Input file(s) (Pass '-' for stdin) -r, --replace Write output in-place, replacing input -o, --outfile Write output to file (default stdout) --config Path to config file --type [js|css|html] ["js"] Select beautifier type (NOTE: Does *not* filter files, only defines which beautifier type to run) -q, --quiet Suppress logging to stdout -h, --help Show this help -v, --version Show the version Beautifier Options: -s, --indent-size Indentation size [4] -c, --indent-char Indentation character [" "] -t, --indent-with-tabs Indent with tabs, overrides -s and -c -e, --eol Character(s) to use as line terminators.
Starred by 8.9K users
Forked by 1.4K users
Languages   JavaScript 42.8% | HTML 35.9% | Python 15.5% | Mustache 2.7% | Shell 2.6% | Makefile 0.3% | CSS 0.2%
🌐
npm
npmjs.com › package › js-beautify
js-beautify - npm
CLI Options: -f, --file Input file(s) (Pass '-' for stdin) -r, --replace Write output in-place, replacing input -o, --outfile Write output to file (default stdout) --config Path to config file --type [js|css|html] ["js"] Select beautifier type (NOTE: Does *not* filter files, only defines which beautifier type to run) -q, --quiet Suppress logging to stdout -h, --help Show this help -v, --version Show the version Beautifier Options: -s, --indent-size Indentation size [4] -c, --indent-char Indentation character [" "] -t, --indent-with-tabs Indent with tabs, overrides -s and -c -e, --eol Character(s) to use as line terminators.
      » npm install js-beautify
    
Published   Feb 27, 2025
Version   1.15.4
Author   Einar Lielmanis
🌐
Kali Linux Tools
en.kali.tools
JS Beautifier - Penetration Testing Tools
html-beautify – improves the readability of HTML code, available only in Node.js JavaScript version ... CLI Options: -f, --file Input file(s) (Pass '-' for stdin) -r, --replace Write output in-place, replacing input -o, --outfile Write output to file (default stdout) --config Path to config ...
🌐
Ariya
ariya.io › 2010 › 09 › offline-command-line-beautifier-for-javascript-code
offline, command-line beautifier for JavaScript code · ariya.io
There are many different ways to autoformat JavaScript code, my favorite is always jsbeautifier.org. Apparently, you can also use it locally without a web browser since it supports running it with Rhino.
🌐
Beautifier
beautifier.io
Online JavaScript beautifier
Beautify JavaScript, JSON, React.js, HTML, CSS, SCSS, and SASS
🌐
Stack Overflow
stackoverflow.com › questions › 41060343 › beautify-html-with-js-beautify-from-command-line
Beautify html with js-beautify from command line - Stack Overflow
According to the readme info at https://github.com/beautify-web/js-beautify (under the heading "Options"), js-beautify can also beautify html and css using the --type command line option.
🌐
GitHub
github.com › googlearchive › js-beautify › tree › master › js-beautify
js-beautify/js-beautify at master · googlearchive/js-beautify
You can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python.
Author   googlearchive
Find elsewhere
🌐
GitHub
github.com › jraoult › js-beautify-converter
GitHub - jraoult/js-beautify-converter: A command line tool to convert JS Beautifier code styles
A command line tool to convert JS Beautifier code styles - jraoult/js-beautify-converter
Author   jraoult
🌐
Mathieu-leplatre
blog.mathieu-leplatre.info › javascript-beautifier-in-command-line-and-in-geany-editor.html
Javascript Beautifier in command-line (and in Geany editor) - Mathieu Leplatre
~/.bin/beautifyjs /your/file.js · Open a Javascript file · Open menu Build > Define Build Commands · Create a new entry (like beautify) In command, enter the following · ~/.bin/beautifyjs %f > /tmp/tmpfile.js && geany /tmp/tmpfile.js · In working directory, enter %d ·
🌐
SourceForge
sourceforge.net › projects › js-beautify.mirror
JS-Beautify download | SourceForge.net
js-beautify is a command-line and Python-based tool that beautifies and formats JavaScript, HTML, and CSS code. It helps improve code readability by enforcing consistent indentation and style rules.
🌐
Codingem
codingem.com › home › how to format javascript code from command line (jsbeautifier)
How to Format JavaScript Code from Command Line (JSBeautifier)
July 10, 2025 - To format JavaScript code from the command line, use the jsbeautifier module. Then call it on a file by js-beautify file_name.js
🌐
Bayern
umweltatlas.bayern.de › mapapps › resources › jsregistry › root › js-beautify › latest › README.md
js-beautify/README.md (latest)
You can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python.
🌐
UNPKG
unpkg.com › browse › js-beautify@0.4.2 › README.md
js-beautify/README.md
```bash $ npm -g install js-beautify ... Options These are the command-line flags for both Python and JS scripts: ```text CLI Options: -f, --file Input file(s) (Pass '-' for stdin)....
🌐
Hexmos
hexmos.com › freedevtools › c › security › js-beautify
JS Beautify - Format & Prettify Code Online - security Cheatsheets | Online Free DevTools by Hexmos
# Beautify HTML content from standard ... file.css | js-beautify -o beautified-file.css · JS Beautify allows for customization of indentation and tab replacement:...
🌐
SourceForge
sourceforge.net › projects › js-beautifier.mirror
JS Beautifier download | SourceForge.net
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. All of the source code is completely free and open, available on GitHub under MIT licence, and we have a command-line version, python library and a node package as well. You may install the NPM package js-beautify.
🌐
GitHub
github.com › rickeyski › node-beautifier
GitHub - rickeyski/node-beautifier: A nodejs-cli for einars {js,css,html}-beautifier
A nodejs-cli for einars {js,css,html}-beautifier. Contribute to rickeyski/node-beautifier development by creating an account on GitHub.
Starred by 29 users
Forked by 10 users
Languages   JavaScript 98.8% | Shell 1.2% | JavaScript 98.8% | Shell 1.2%