<p>I am writing a batch script in order to beautify JavaScript code. It needs to work on both <strong>Windows</strong> and <strong>Linux</strong>. </p>

<p>How can I beautify JavaScript code using the command line tools? </p>

[{'answer_id': 27343, 'author': 'Alan Storm', 'author_id': 2838, 'author_profile': 'https://Stackoverflow.com/users/2838', 'pm_score': 7, 'selected': True, 'text': '<p>First, pick your favorite Javascript based Pretty Print/Beautifier.  I prefer the one at <a href="http://jsbeautifier.org/" rel="noreferrer"><a href="http://jsbeautifier.org/" rel="noreferrer">http://jsbeautifier.org/</a></a>, because it\'s what I found first.  Downloads its file <a href="https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js" rel="noreferrer">https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js</a></p>\n\n<p>Second, download and install The Mozilla group\'s Java based Javascript engine, <a href="https://www.mozilla.org/rhino/" rel="noreferrer">Rhino</a>.  "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 </p>\n\n<pre><code>java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js\n</code></pre>\n\n<p>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</p>\n\n<pre><code>//original code    \n(function() { ... js_beautify code ... }());\n\n//new code\nprint(global.js_beautify(readFile(arguments[0])));\n</code></pre>\n\n<p>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.   </p>\n\n<p>You\'d invoke the above something like </p>\n\n<pre><code>java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 4041563, 'author': 'knb', 'author_id': 202553, 'author_profile': 'https://Stackoverflow.com/users/202553', 'pm_score': 6, 'selected': False, 'text': '<p><strong>UPDATE April 2014</strong>:</p>\n<p>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 <a href="https://github.com/einars/js-beautify/" rel="noreferrer">project page on github.com</a>.</p>\n<p>Python style:</p>\n<pre><code> $ pip install jsbeautifier\n</code></pre>\n<p>NPM style:</p>\n<pre><code> $ npm -g install js-beautify\n</code></pre>\n<p>to use it (this will return the beatified js file on the terminal, the main file remains unchanged):</p>\n<pre><code> $ js-beautify file.js\n</code></pre>\n<p>To <strong>make the changes take effect on the file</strong>, you should use this command:</p>\n<pre class="lang-sh prettyprint-override"><code>$ js-beautify -r file.js\n</code></pre>\n<p><strong>Original answer</strong></p>\n<p>Adding to Answer of @Alan Storm</p>\n<p>the command line beautifier based on <a href="http://jsbeautifier.org/" rel="noreferrer">http://jsbeautifier.org/</a>  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 &quot;js.jar&quot;).  So you can use V8 instead of rhino.</p>\n<p>How to use:</p>\n<p>download jsbeautifier.org zip file from\n<a href="http://github.com/einars/js-beautify/zipball/master" rel="noreferrer">http://github.com/einars/js-beautify/zipball/master</a></p>\n<p>(this is a download URL linked to a zip file such as <a href="http://download.github.com/einars-js-beautify-10384df.zip" rel="noreferrer">http://download.github.com/einars-js-beautify-10384df.zip</a>)</p>\n<p>old (no longer works, jar file is gone)</p>\n<pre><code>  java -jar js.jar  name-of-script.js\n</code></pre>\n<p>new (alternative)</p>\n<p>install/compile v8 lib FROM svn, see v8/README.txt in above-mentioned zip file</p>\n<pre><code>  ./jsbeautify somefile.js\n</code></pre>\n<p>-has slightly different command line options than the rhino version,</p>\n<p>-and works great in Eclipse when configured as an &quot;External Tool&quot;</p>\n'}, {'answer_id': 4065400, 'author': 'Shonzilla', 'author_id': 31625, 'author_profile': 'https://Stackoverflow.com/users/31625', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve written an article explaining how to build a <a href="http://blog.shonzilla.com/post/1448821411/command-line-javascript-beautifier" rel="nofollow">command-line JavaScript beautifier implemented in JavaScript</a> in under 5 minutes. YMMV.</p>\n\n<blockquote>\n  <ol>\n  <li>Download the latest stable Rhino and unpack it somewhere, e.g. ~/dev/javascript/rhino</li>\n  <li>Download beautify.js which is referenced from aforementioned jsbeautifier.org then copy it somewhere, e.g. ~/dev/javascript/bin/cli-beautifier.js</li>\n  <li><p>Add this at the end of beautify.js (using some additional top-level properties to JavaScript):</p>\n\n<pre><code>// Run the beautifier on the file passed as the first argument.\nprint( j23s_beautify( readFile( arguments[0] )));\n</code></pre></li>\n  <li><p>Copy-paste the following code in an executable file, e.g. ~/dev/javascript/bin/jsbeautifier.sh:</p>\n\n<pre><code>#!/bin/sh\njava -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*\n</code></pre></li>\n  <li><p>(optional) Add the folder with jsbeautifier.js to PATH or moving to some folder already there.</p></li>\n  </ol>\n</blockquote>\n'}, {'answer_id': 14545400, 'author': 'ioikka', 'author_id': 1662291, 'author_profile': 'https://Stackoverflow.com/users/1662291', 'pm_score': 2, 'selected': False, 'text': '<p>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. </p>\n\n<p>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 (it wraps the jsbeautifier.org beautifier .js script but is callable from java or the command line).</p>\n\n<p>The code is located at <a href="https://github.com/belgampaul/JsBeautifier" rel="nofollow">https://github.com/belgampaul/JsBeautifier</a></p>\n\n<p>I used rhino and beautifier.js</p>\n\n<p>USAGE from console: java -jar jsbeautifier.jar script indentation</p>\n\n<p>example: java -jar jsbeautifier.jar "function ff() {return;}" 2</p>\n\n<p>USAGE from java code:\npublic static String jsBeautify(String jsCode, int indentSize)</p>\n\n<p>You are welcome to extend the code. In my case I only needed the indentation so I could check the generated javascript while developing.</p>\n\n<p>In the hope it\'ll save you some time in your project.</p>\n'}, {'answer_id': 16513195, 'author': 'erapert', 'author_id': 1411115, 'author_profile': 'https://Stackoverflow.com/users/1411115', 'pm_score': 5, 'selected': False, 'text': '<p>If you\'re using nodejs then try <a href="https://github.com/mishoo/UglifyJS/" rel="noreferrer">uglify-js</a></p>\n\n<p>On Linux or Mac, assuming you already have nodejs installed, you can install uglify with:</p>\n\n<blockquote>\n  <p><code>sudo npm install -g uglify-js</code></p>\n</blockquote>\n\n<p>And then get the options:</p>\n\n<blockquote>\n  <p><code>uglifyjs -h</code></p>\n</blockquote>\n\n<p>So if I have a source file <code>foo.js</code> which looks like this:</p>\n\n<pre><code>// foo.js -- minified\nfunction foo(bar,baz){console.log("something something");return true;}\n</code></pre>\n\n<p>I can beautify it like so:</p>\n\n<blockquote>\n  <p><code>uglifyjs foo.js --beautify --output cutefoo.js</code></p>\n</blockquote>\n\n<p><code>uglify</code> uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it through <code>unexpand</code> which Ubuntu 12.04 comes with:</p>\n\n<blockquote>\n  <p><code>unexpand --tabs=4 cutefoo.js &gt; cuterfoo.js</code></p>\n</blockquote>\n\n<p>Or you can do it all in one go:</p>\n\n<blockquote>\n  <p><code>uglifyjs foo.js --beautify | unexpand --tabs=4 &gt; cutestfoo.js</code></p>\n</blockquote>\n\n<p>You can find out more about unexpand <a href="https://stackoverflow.com/questions/1424126/replace-whitespaces-with-tabs-in-linux">here</a></p>\n\n<p>so after all this I wind up with a file that looks like so:</p>\n\n<pre><code>function foo(bar, baz) {\n    console.log("something something");\n    return true;\n}\n</code></pre>\n\n<p><strong>update 2016-06-07</strong></p>\n\n<p>It appears that the maintainer of uglify-js is now working on <a href="https://github.com/mishoo/UglifyJS2" rel="noreferrer">version 2</a> though installation is the same.</p>\n'}, {'answer_id': 18343340, 'author': 'humkins', 'author_id': 1902296, 'author_profile': 'https://Stackoverflow.com/users/1902296', 'pm_score': 1, 'selected': False, 'text': '<p>I believe when you asked about command line tool you just wanted to beautify all your js files in batch. </p>\n\n<p>In this case Intellij IDEA (tested with 11.5) can do this.</p>\n\n<p>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".\nJust make sure you dedicated enough memory for the JVM.</p>\n'}, {'answer_id': 21812204, 'author': 'tmaric', 'author_id': 704028, 'author_profile': 'https://Stackoverflow.com/users/704028', 'pm_score': 2, 'selected': False, 'text': '<p>In the console, you can use <a href="http://astyle.sourceforge.net/" rel="nofollow">Artistic Style</a> (a.k.a. AStyle) with <code>--mode=java</code>.<br>\nIt works great and it\'s free, open-source and cross-platform (Linux, Mac OS X, Windows).</p>\n'}, {'answer_id': 25855828, 'author': 'Tchakabam', 'author_id': 589493, 'author_profile': 'https://Stackoverflow.com/users/589493', 'pm_score': 2, 'selected': False, 'text': '<p>Use the modern JavaScript way:</p>\n\n<p>Use <a href="http://gruntjs.com/" rel="nofollow">Grunt</a> in combination with the <a href="https://www.npmjs.org/package/grunt-jsbeautifier" rel="nofollow">jsbeautifier plugin for Grunt</a> </p>\n\n<p>You can install everything easily into your dev environment using <a href="https://www.npmjs.org/" rel="nofollow">npm</a>.</p>\n\n<p>All you will need is set up a Gruntfile.js with the appropriate tasks, which can also involve file concatenation, lint, uglify, minify etc, and run the grunt command.</p>\n'}, {'answer_id': 55358995, 'author': 'Serge Stroobandt', 'author_id': 2192488, 'author_profile': 'https://Stackoverflow.com/users/2192488', 'pm_score': 4, 'selected': False, 'text': '<h1>On Ubuntu LTS</h1>\n<pre><code>$ sudo apt install jsbeautifier\n$ js-beautify ugly.js &gt; beautiful.js\n</code></pre>\n<p>For in place beautifying, any of the follwing commands:</p>\n<pre><code>$ js-beautify -r file.js\n$ js-beautify --replace file.js\n</code></pre>\n'}, {'answer_id': 63420552, 'author': 'Alex Nolasco', 'author_id': 65694, 'author_profile': 'https://Stackoverflow.com/users/65694', 'pm_score': 3, 'selected': False, 'text': '<p>You have a few one liner choices. Use with npm or standalone with <a href="https://stackoverflow.com/questions/50605219/difference-between-npx-and-npm">npx</a>.</p>\n<p><a href="https://github.com/standard/semistandard" rel="noreferrer">Semistandar</a></p>\n<pre><code>npx semistandard &quot;js/**/*.js&quot; --fix\n</code></pre>\n<p><a href="https://standardjs.com/" rel="noreferrer">Standard</a></p>\n<pre><code>npx standard &quot;js/**/*.js&quot; --fix\n</code></pre>\n<p><a href="https://prettier.io/" rel="noreferrer">Prettier</a></p>\n<pre><code>npx prettier --single-quote --write --trailing-comma all &quot;js/**/*.js&quot;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/18985', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/486/']