For the complete experience, please enable JavaScript in your browser. Thank you!

  • Creative Cloud
  • Photoshop
  • Illustrator
  • InDesign
  • Premiere Pro
  • After Effects
  • Lightroom
  • See all
  • See plans for: businesses photographers students
  • Document Cloud
  • Acrobat DC
  • eSign
  • Stock
  • Elements
  • Marketing Cloud
  • Analytics
  • Audience Manager
  • Campaign
  • Experience Manager
  • Media Optimizer
  • Target
  • See all
  • Acrobat Reader DC
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player
  • All products
  • Creative Cloud
  • Individuals
  • Photographers
  • Students and Teachers
  • Business
  • Schools and Universities
  • Marketing Cloud
  • Document Cloud
  • Stock
  • Elements
  • All products
  • Get Support
    Find answers quickly. Contact us if you need to.
    Start now >
  • Learn the apps
    Get started or learn new ways to work.
    Learn now >
  • Ask the community
    Post questions and get answers from experts.
    Start now >
    • About Us
    • Careers At Adobe
    • Investor Relations
    • Privacy  |  Security
    • Corporate Responsibility
    • Customer Showcase
    • Events
    • Contact Us
News
    • 2/16/2016
      Adobe Announces New ColdFusion for Creating High-Performing Web and Mobile Apps
    • 2/10/2016
      Adobe Data Science Symposium and Grants Bring Big Ideas to Big Data
    • 2/3/2016
      Media Alert: Adobe to Unveil Next Generation Marketing Cloud at Summit
    • 1/21/2016
      Adobe Video Tools Get Rave Reviews at Sundance 2016
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
vat included
Purchase requires verification of academic eligibility
Subtotal
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
Review and Checkout
Adobe Developer Connection /

Node.js command-line tools for front-end development

by Brian Rinaldi

Brian Rinaldi
  • remotesynthesis.com

Content

  • UglifyJS
  • Grunt
  • GruntIcon
  • JSHint
  • Node-static
  • Where to go from here

Created

21 January 2013

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
JavaScript
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

Requirements

Prerequisite knowledge

Experience working with the command line is required. Some knowledge of Node.js will help you make the most of this article.

 

Required products

Node.js

User level

Intermediate

You may already know that Node.js enables you to create server-side applications using JavaScript. While this is a powerful and compelling capability, perhaps you aren’t a server-side developer or you aren’t yet ready to commit to server-side development using JavaScript. You may not yet be aware, however, that Node.js has also become the platform behind a growing number of command-line utilities that can run on all platforms on which Node runs and that are incredibly useful for front-end developers, regardless of their need for, or interest in, server-side JavaScript. In fact, there are a ton of command-line interface (CLI) utilities already out there for Node. In this article I discuss five Node CLI utilities that can help improve your front-end development workflow.

UglifyJS

UglifyJS is a JavaScript code beautifier and compressor, among other things, that is available as a node package manager (NPM) module. Assuming you have Node.js installed, UglifyJS can be installed simply by typing npm install uglify-js at the command line. Obviously, you will need to write JavaScript code for this tool to be useful; however you do not need to be writing code intended for a Node server.

If you are curious about what UglifyJS will generate as output, you can test it out via the browser. UglifyJS has many more options available via the command line as well as via its JavaScript API.

After you've installed UglifyJS with npm, you can use the following steps to try it out by executing the following command:

uglifyjs -b -o <output file> <input file>

For example, I executed:

uglifyjs -b -o C:\Users\brinaldi\Documents\projects\brinaldi-phonegap-workshop\step5\js\index-clean.js C:\Users\brinaldi\Documents\projects\brinaldi-phonegap-workshop\step5\js\index.js

The -b option beautifies the code and the -o option is used to specify the output file name. (If you want it to simply overwrite your existing file, you can use --overwrite, though to me that seems like a potentially bad idea.)

The original version of the file I used is a simple JavaScript file from a sample jQuery Mobile PhoneGap application and is available at http://pastebin.com/9skqS8VL and the result file is at http://pastebin.com/WRXXeL1a.

Admittedly, this is a simple file (and it could use some manual cleanup) but the resulting file is 52 lines shorter and arguably more readable than the original.

Grunt

Grunt is a build tool for JavaScript development that simplifies several common tasks. It can generate project scaffolding based on a number of predefined but configurable templates, run tests (using QUnit and PhantomJS), lint files (using JSHint), minify files (using UglifyJS), and start a static web server, among other tasks.

While many of the default templates are very much geared towards developing applications on a Node server, there is a jQuery template for building jQuery plugins as well as a default template that includes just basic files needed by Grunt. In addition, there are numerous community contributed Grunt plugins to handle a huge list of tasks.

To see how it works, you can use the following steps to generate the jQuery plugin files:

  1. Install Grunt via NPM using the following command:
npm install -g grunt
  1. Navigate to the folder you want to hold the files, and then execute the following command:
grunt init:jquery
  1. Provide answers to the quests that Grunt asks. It needs these answers before it can properly generate the files.

Grunt will then generate your files based on the responses you provided (see Figure 1).

Figure 1. Grunt output on Windows.
Figure 1. Grunt output on Windows.

You can now edit your generated files and configure and use other commands like grunt test or grunt lint. You can get help with grunt --help.

Note: After you've generated files via Grunt, on subsequent runs Grunt will try to read the grunt.js file in the directory for configuration settings. On Windows, you'll need to type grunt.cmd to run the command rather than simply grunt, which will invoke grunt.js in order to prevent Windows from trying to open the js file.

For a more detailed tutorial on Grunt, check out the Meet Grunt: The Build Tool for JavaScript  tutorial from Nettuts+. If you like Grunt, you should also check out Yeoman, which is similar to Grunt but includes a number of additional features. I haven't covered Yeoman in detail here because it doesn't currently support Windows.

GruntIcon

GruntIcon, which depends on Grunt, is a Grunt task that converts SVG files for your web page icons (including those developed in Illustrator) into PNG files while also creating stylesheets with a CSS class references for each icon generated.

To test this out, you can use the SVG files provided in the GruntIcon example source.

  1. Download the project zip or check it out of GitHub.
  2. Copy the SVG icon files to a new directory you create called src/icons-source/ within the Grunt project from the previous section.
  3. Change to your Grunt project's home directory and install GruntIcon by executing the following command:
npm install grunt-grunticon
  1. GruntIcon requires PhantomJS, so download and install that as well. PhantomJS needs to be executable via the command phantomjs at the command line. To enable this on Windows, you either need to add it to your path or, for a quick and dirty solution, simply put the phantomjs.exe file in the project folder.
  2. Edit grunt.js. Load GruntIcon as a task inside your grunt.js file by adding the following line (I added it right after the default task line near the end of the document):
// Default task. grunt.registerTask('default', 'lint qunit concat min'); grunt.loadNpmTasks('grunt-grunticon');
  1. Next you need to define the Grunt task for GruntIcon, by simply supplying the source folder and the output folder (I placed this after the empty uglify task, adding a comma following uglify: {}):

uglify: {}, grunticon: { src: "src/icons-source/", dest: "src/icons-output/" }
  1. Now that our task is defined, you can run it from the command line:
grunt grunticon

If you're on Windows use:

grunt.cmd grunticon

The output folder now contains a folder with PNG files and multiple CSS files as well as an example HTML file that displays all the generated icons (see Figure 2).

Figure 2. Running grunticon
Figure 2. Running grunticon

JSHint

JSHint, like its well-known counterpart JSLint, is a JavaScript code linter that can be used to detect possible problems with your code and to enforce code standards. Also like JSLint, it is highly configurable either through inline comments in your JavaScript files or via a configuration file. It is, however, far less strict than JSLint by default. To see the kind of output that it provides, you can use the web-based version of JSHint. Several text editors have freely available plugins for JSHint, including Brackets, which uses JSLint by default.

Follow these steps to use JSHint to check the main.js JavaScript file from the previous UglifyJS example (keep in mind, this is not a complex file):

  1. Install JSHint via the command line:
npm install jshint -g
  1. Change to the project's directory via the command line.
  2. Execute the following command:
jshint <file to check>

For example, I executed the following:

jshint js/index.js

Note that you can also use JSHint to check an entire directory and it will report on every .js file it finds.

  1. Examine the output for potential problems in your JavaScript code (see Figure 3).
Figure 3. JSHint output before and after fixing issues it reports.  For the final run, JSHint returns no results as all the problems have been corrected.
Figure 3. JSHint output before and after fixing issues it reports. For the final run, JSHint returns no results as all the problems have been corrected.

Node-static

With node-static you can quickly and easily start up a local web server in any directory on your machine. Sometimes, when developing and testing your applications, you need to use a web server. For instance, certain HTML5 APIs and features (for example, CORS) do not run using the file:/// protocol in the browser. In these cases, being able to start a local server instance, or even multiple instances on different ports, can be helpful.

Follow these steps to get started:

  1. To install node-static, execute the following command:
npm install -g node-static
  1. Via the command line, navigate to the folder in which you would like to start up the local server.
  2. Execute the following command:
static

By default the port is 8080 (see Figure 4). You can specify a different port, which is needed if you want to run multiple servers in different folders at the same time. You can also specify cache settings and headers.

Figure 4. Starting node-static in the …\brinaldi-phonegap-workshop\step5 directory.
Figure 4. Starting node-static in the …\brinaldi-phonegap-workshop\step5 directory.

Where to go from here

This article has covered only a handful of the many Node command-line utilities available. You may also want to explore bunyip for client-side unit testing (for a good tutorial on this, see Cross Browser Testing with bunyip by Jack Franklin), Underscore-CLI for parsing and manipulating JSON data, and Dox for generating documentation.

If you are curious about additional Node CLI tools, you can view the current list of all Node modules, Smashing Magazine's comprehensive list of Node resources, and Nodejitsu's short list of Node CLI applications.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Using the Geolocation API
  • HTML5 basics
  • Introducing theexpressiveweb.com beta
  • Developing HTML5 games with Impact JavaScript game engine and Dreamweaver CS5.5
  • Introducing the HTML5 storage APIs
  • JavaScript object creation: Learning to live without "new"
  • Working with HTML5 multimedia components – Part 3: Custom controls
  • Extending PhoneGap with native plugins for Android
  • Extending PhoneGap with native plugins for iOS
  • Getting started with PhoneGap in Xcode for iOS
Choose your region United States (Change)   Products   Downloads   Learn & Support   Company
Choose your region Close

Americas

Europe, Middle East and Africa

Asia Pacific

  • Brasil
  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Cyprus - English
  • Česká republika
  • Danmark
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Greece - English
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Malta - English
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Southeast Asia (Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam) - English
  • 台灣

Commonwealth of Independent States

  • Includes Armenia, Azerbaijan, Belarus, Georgia, Moldova, Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Ukraine, Uzbekistan

Copyright © 2016 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

AdChoices