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"

🌐
Format.JS
formatjs.github.io › docs › tooling › cli
Cli | FormatJS
Take a look at our builtin formatter code for some examples. Install @formatjs/cli-lib instead to use programmatically
🌐
npm
npmjs.com › package › cli-format
cli-format - npm
A command line formatting utility that makes it easy to manage word wrapping, indents, padding, columns, and more. Also works with ansi-encoding libraries chalk, colors, and others..
      » npm install cli-format
    
Published   Nov 09, 2016
Version   3.0.9
Author   James Speirs
🌐
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 ·
🌐
GitHub
github.com › beautifier › js-beautify
GitHub - beautifier/js-beautify: Beautifier for javascript · GitHub
To use js-beautify as a node library (after install locally), import and call the appropriate beautifier method for JavaScript (JS), CSS, or HTML. All three method signatures are beautify(code, options). code is the string of code to be beautified. options is an object with the settings you would like used to beautify the code. The configuration option names are the same as the CLI names but with underscores instead of dashes.
Author   beautifier
🌐
Oxidation Compiler
oxc.rs › docs › guide › usage › formatter.html
Oxfmt | The JavaScript Oxidation Compiler
Oxfmt includes built-in features that typically require external Prettier plugins: ... Oxfmt integrates into existing Prettier-based workflows. The oxfmt CLI behaves similarly to Prettier by default, allowing adoption with minimal changes to ...
🌐
npm
npmjs.com › package › js-beautify
js-beautify - npm
To use js-beautify as a node library (after install locally), import and call the appropriate beautifier method for JavaScript (JS), CSS, or HTML. All three method signatures are beautify(code, options). code is the string of code to be beautified. options is an object with the settings you would like used to beautify the code. The configuration option names are the same as the CLI names but with underscores instead of dashes.
      » npm install js-beautify
    
Published   Feb 27, 2025
Version   1.15.4
Author   Einar Lielmanis
Find elsewhere
🌐
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 file --type [js|css|html] ["js"] -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.
🌐
Beautifier
beautifier.io
Online JavaScript beautifier
Emacs: Web-beautify formatting package by Yasuyuki Oka, Komodo IDE: Beautify-js addon by Bob de Haas (github), C#: ghost6991 ported the javascript formatter to C#,
🌐
GitHub
github.com › millermedeiros › esformatter
GitHub - millermedeiros/esformatter: ECMAScript code beautifier/formatter
var esformatter = require('esformatter'); var fs = require('fs'); var codeStr = fs.readFileSync('path/to/js/file.js').toString(); // for a list of available options check "lib/preset/default.js" var options = { indent : { value : ' ' }, lineBreak : { before : { // at least one line break before BlockStatement BlockStatement : '>=1', // only one line break before DoWhileStatementOpeningBrace DoWhileStatementOpeningBrace : 1, // ... } }, whiteSpace : { // ... } }; // return a string with the formatted code var formattedCode = esformatter.format(codeStr, options); See the doc/api.md file for a li
Starred by 968 users
Forked by 90 users
Languages   JavaScript
🌐
GitHub
github.com › ioikka › JsBeautifier
GitHub - ioikka/JsBeautifier: Simple JavaScript formatter. Indents a javascript string with the indent size passed as a parameter. Uses rhino and http://jsbeautifier.org/beautify.js to achieve this
Simple JavaScript formatter. Indents a javascript string with the indent size passed as a parameter. Uses rhino and http://jsbeautifier.org/beautify.js to achieve this - ioikka/JsBeautifier
Starred by 2 users
Forked by 2 users
🌐
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.
🌐
npm
npmjs.com › package › @formatjs › cli
@formatjs/cli - npm
A CLI for formatjs.. Latest version: 6.13.0, last published: 9 days ago. Start using @formatjs/cli in your project by running `npm i @formatjs/cli`. There are 44 other projects in the npm registry using @formatjs/cli.
      » npm install @formatjs/cli
    
Published   Feb 19, 2026
Version   6.13.0
Author   Linjie Ding
🌐
GitHub
github.com › flatiron › cliff
GitHub - flatiron/cliff: Your CLI formatting friend
var cliff = require('../lib/cliff'); var rows = [ ['Name', 'Flavor', 'Dessert'], ['Alice', 'cherry', 'yogurt'], ['Bob', 'carmel', 'apples'], ['Joe', 'chocolate', 'cake'], ['Nick', 'vanilla', 'ice cream'] ]; console.log(cliff.stringifyRows(rows, ['red', 'blue', 'green'])); ... The putRows method is a simple helper that takes a set of Arrays and row headers and logs properly formatted and padded rows (logs stringifyRows to winston).
Starred by 193 users
Forked by 15 users
Languages   JavaScript
🌐
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 = JSON.stringify(obj); document.getElementById("insurance").innerHTML = carInsuranceCompanyObj; ... Buy us a Coffee JSON Formatter FAQ Privacy Policy Content Policy About Contact History Where am I right now?
🌐
Medium
medium.com › scripting-bits › formatting-code-from-the-command-line-8102abea6762
Formatting Code From The Command Line | by Vinson Chuong | Scripting Bits | Medium
March 12, 2020 - My first thought was to look for a CLI tool published on npm and then run it with npx. I know that jq is great for JSON and Prettier is great for JavaScript.