Take the 2-minute tour ×

I am looking for a code beautifier that supports javascript and works on both windows and linux and can be used in batch scripts. Any recommendations?

share|improve this question
add comment

16 Answers

up vote 49 down vote accepted

First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it's what I found first.

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 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

//new code

//original code    
function js_beautify(js_source_text, indent_size, etc...

Rhino gives javascript a few extra useful functions that don't necesarily 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 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.

share|improve this answer
the link you have given is broken, it should be this one i think, jsbeautifier.org –  Sinan Yasar Jan 12 '10 at 13:26
Sinan is right. The link moved to jsbeautifier.org –  Jon Adams Jan 15 '10 at 20:17
How I did it: :~$ sudo apt-get install libv8-dev libv8-2.2.18 :~$ cd einars-js-beautify-f90ce72/v8 :~$ g++ -o jsbeautify jsbeautify.cpp -lv8 -Llib -lpthread It just works. Thanks to Einar Lielmanis and everyone involved! –  Berov Mar 6 '11 at 13:45
These seems to be a moving target. Now the info on jsbeautifier site says it's written in python. –  seth Apr 26 '11 at 3:57
update for cmdline users, the following repo has easy to install stand alone command line tools for pretty printing js: github.com/einars/js-beautify.git –  drhodes Aug 29 '12 at 19:31
add comment

My Pretty Diff tool is written entirely in JavaScript so it works equally well on all operating systems. It supports beautification and minification of JavaScript, CSS, any markup language that uses XML style delimiters, including HTML.


share|improve this answer
add comment

So far I have found a couple of online ones:

Still looking for something I can ran from the command line.

share|improve this answer
I assume you've found what you need by now (4 years later), but there's always python's json.tool: richardlog.com/post/12743073497/… –  Steve Armstrong Mar 27 '12 at 16:59
add comment

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")

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 (still works)

  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"

share|improve this answer
Or if you prefer python over v8, there's also a python module in there now. –  keturn Oct 4 '11 at 0:15
add comment

If you're using nodejs then try uglify-js

On Ubuntu 12.04, assuming you already have nodejs installed, you can install uglify with:

sudo npm install -g uglify-js

And then get the options:

uglifyjs -h

So if I have a source file foo.js which looks like this:

function foo(bar,baz){console.log("something something");return true;}

I can beautify it like so:

uglifyjs foo.js --beautify --output cutefoo.js

uglify uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it through unexpand which Ubuntu 12.04 comes with:

unexpand --tabs=4 cutefoo.js > cuterfoo.js

You can find out more about unexpand here

so after all this I wind up with a file that looks like so:

function foo(bar, baz) {
    console.log("something something");
    return true;
share|improve this answer
add comment

This is a very useful website to beautify/indent your js files. i often use it. Check this: http://jsbeautifier.org/

share|improve this answer
add comment

I use this one (http://jsbeautifier.atomproject.net/) beautifier. Its online and free.

share|improve this answer
add comment

I'm not able to add a comment to the accepted answer so that's why you see a post that should have not existed in the first place.

Basically I also needed a javascript beautifier in a java code and to my surprise none is available as far as I could find. So I coded one myself entirely based on the accepted answer.

The code is located at https://github.com/belgampaul/JsBeautifier

I used rhino and beautifier.js

USAGE from console: java -jar jsbeautifier.jar script indentation

example: java -jar jsbeautifier.jar "function ff() {return;}" 2

USAGE from java code: public static String jsBeautify(String jsCode, int indentSize)

You are welcome to extend the code. In my case I only needed the indentation so I could check the generated javascript while developing.

In the hope it'll save you some time in your project.

share|improve this answer
add comment

The problem with allot of beautifiers is the choice of output and filtering and the sever lack of serious command line beautifiers. I've recently begun modifying YUI compressor to beautify code. If you want to make your own beautifier I have to highly recommend this approach. YUI is open source and comes with an ant build file making it very easy to alter and compile.

share|improve this answer
add comment

Here's a javascript beautifier written in .NET supporting command-line as well as interactive mode: http://www.rahulsingla.com/blog/2010/12/jsbeautifier-net-javascript-beautifier-in-net

There's no external dependency except .Net 2.0.

share|improve this answer
add comment

I've written an article explaining how to build a command-line JavaScript beautifier implemented in JavaScript in under 5 minutes. YMMV.

  1. Download the latest stable Rhino and unpack it somewhere, e.g. ~/dev/javascript/rhino
  2. Download beautify.js which is referenced from aforementioned jsbeautifier.org then copy it somewhere, e.g. ~/dev/javascript/bin/cli-beautifier.js
  3. Add this at the end of beautify.js (using some additional top-level properties to JavaScript):

    // Run the beautifier on the file passed as the first argument.
    print( j23s_beautify( readFile( arguments[0] )));
  4. Copy-paste the following code in an executable file, e.g. ~/dev/javascript/bin/jsbeautifier.sh:

    java -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*
  5. (optional) Add the folder with jsbeautifier.js to PATH or moving to some folder already there.

share|improve this answer
add comment

If you use notepad++ here is a plugin for notepad++ http://www.sunjw.us/jsminnpp/

share|improve this answer
add comment

I believe when you asked about command line tool you just wanted to beautify all your js files in batch.

In this case Intellij IDEA (tested with 11.5) can do this.

You just need to select any of your project files and select "Code"->"Reformat code.." in main IDE menu. Then in the dialog select "all files in directory ..." and press "enter". Just make sure you dedicated enough memory for the JVM.

share|improve this answer
add comment

In the console, astyle can be used, with --mode=java. It works great and is available on many Linux platforms.

share|improve this answer
add comment

Depending upon how well you script sometimes its a good idea to inspect your code before you compact it. I suggest JSLint, personally. Browsers are made to eat such crap, frequently I find myself working C# and forgetting some minor JavaScript items.

For instance, I never tended to use ===/!== when comparing variables against null, true, false, 0, or "", but apparently its a best practice over ==/!= since it doesn't employ typecasting.

A VisualStudio 2008 JSLint plugin' would rule the house.

share|improve this answer
umm, how is this even remotely related to the question and why is it getting upvoted?! on another note: jslint.codeplex.com –  Sky Sanders Apr 8 '10 at 19:00
Chirpy gives you JSHint in VS –  Webveloper May 20 '12 at 4:23
not an answer to the question –  oligofren Mar 12 at 10:25
add comment

This is a free stand-alone beautifier (and much more):


share|improve this answer
add comment

protected by balexandre Jun 11 '12 at 19:57

