longchamp
tory burch outlet
tory burch outlet
ray ban australia
Babyliss
nike air max
Michael Kors outlet
toms outlet
nike air max
louis vuitton outlet
nike air max
coach outlet
nike air max
ray ban uk
oakley sunglasses
Michael Kors outlet
Minify JavaScript using UglifyJS and NodeJS

Minify JavaScript using UglifyJS and NodeJS

On January 10, 2012, in NodeJS, by jonathancreamer

Minifying JavaScripts has many benefits. A few advantages are it reduces the amount of code the user has to download, removes unnecessary comments, and reduces the number of Http requests. There are many minify-ers out there, things such as YUI Compressor, JSMin, Google Closure Compiler, and UglifyJS.

I recently started using the standalone UglifyJS tool that can run in NodeJS. This makes the tool available via command prompt as well as accessing it inside of Node applications with require.

To use Uglify in Windows. First, download and install NodeJS. Once it is installed, you can then run applications in Command Prompt simply by typing node. It installs by default into C:\Program Files\NodeJS or C:\Program Files(x86)\NodeJS on a 64-bit machine.

It drops the node.exe, and the npm.bat file into that directory and makes it available in CMD by adding it to your %PATH%.

NPM is the Node Package Manager that was just recently made available for Windows. It allows you to download Node packages to be easily used in your projects.

The next step is to install UglifyJS and make it global so it’s available everywhere.

Open Command Prompt and run…

npm -g install uglify-js

This put’s the files for Uglify in the %AppData% file, usually around C:\Users\{username}\AppData\Roaming\npm\

As of 1/9/2012, there is a slight bug in the command to actually run Uglify, so in order to fix it, navigate to the folder just mentioned and find uglifyjs.cmd and replace it with…

:: Created by npm, please don't edit manually.
@IF EXIST "%~dp0"\"node.exe" (
  "%~dp0"\"node.exe"  "%~dp0\.\node_modules\uglify-js\bin\uglifyjs" %*
) ELSE (
  node  "%~dp0\.\node_modules\uglify-js\bin\uglifyjs" %*
)

Now, you will be able to just run Uglify by…

cd c:\place\where\files\are\
uglifyjs -o myFile.build.js myFile.js

And wham bam, you’ll find a nicely compressed file. NOTE: You also may need to restart the computer. Sometimes the changes to the path don’t kick in right away. You can string along as many files as you need to compress; the –o flag tells Uglify where to place the built file. Then you could save the commands out to some build.bat file and run it at your plessure.

Tagged with:  
  • Pingback: Minify Javascript using UglifyJS and NodeJS « Jonathan Creamer's Dev Diary

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1018

  • http://twitter.com/PabloSerbo Paul Serby

    You can do this seamlessly in Express using: https://github.com/serby/compact

    • Anonymous

      Very nice! Thanks.

  • nguyenle

    very good article. It works well
    another tool maybe help is http://marijnhaverbeke.nl

  • Guest

    Thank you for this short crashcourse on how to install/use uglifyjs. There are not many tutorials that explain the start from the scratch. Yours is the first I found.
    Btw: calling uglifyjs in the cmd from everywhere seems to work now without any changes on the .cmd file.

  • http://hannes-schurig.de/ Hannes Schurig

    Thank you for this short crashcourse on how to install/use uglifyjs. There are not many tutorials that explain the start from the scratch. Yours is the first I found.
    Btw: calling uglifyjs in the cmd from everywhere seems to work now without any changes on the .cmd file

    Sorry about double posting, can you please delete the first comment, I did something wrong there.

  • Howard

    Great explanation for how to get this setup on your own box. Keep doing more articles like this one as there are not many basics ones out there it seems, as @hannesschurig:disqus mentioned as well.

    Though I am a tad lazy at keeping things like that up to date on my own box. I generally use the online tool at http://www.blimptontech.com it uses UglifyJS2 and seems to always be updated as new upstream releases come out. Where as most other online tools are locked in time or are just broken.