I am Hack Sparrow
Captain of the Internets.

JSLint from the Command Line

Maybe you have heard about JSLint, or maybe not. If you haven't yet, it is a popular JavaScript tool authored by Douglas Crockford, which helps you optimize your JavaScript code by telling you the problem areas and suggesting solutions for the same. Quite a nifty tool, try it here.

Despite it's usefulness, the JSLint as we know it, has a little problem: it is an online tool, hosted on Crockford's server.

Wouldn't it be wonderful if JSLint were a command line tool?

Good news! JSLint is now available as a command line tool and in this tutorial I will show you how to use it for linting you local JavaScript files.

Install the JSLint Node.js module:

$ sudo npm install jslint -g

With that, JSLint command line is installed on your system. It's just a matter of invoking the jslint command now. I will show you how to do so using some examples.

To lint a single file named application.js:

$ jslint application.js 

To lint all the JavaScript files in the current directory:

$ find . -name '*.js' -print0 | xargs -0 jslint

To lint all the JavaScript files in a targeted directory:

$ find ./public/javascripts/ -name '*.js' -print0 | xargs -0 jslint

JSLint accepts more than a dozen options, you can find them in under the Options tab on http://www.jslint.com.

To pass JSLint parameters for a single file:

$ jslint --strict --white application.js 

To pass JSLint parameters for all the files in the current directory:

$ find . -name '*.js' -print0 | xargs -0 jslint --strict --white

Parameter-less options passed to jslint are all set to true by default, if you want to set it false, just specify 'false' after the option.

Here is an example of setting an option to false:

$ jslint --bitwise false application.js 

There you have it, JSLint right at your command line. If you aren't already, I would highly recommend you use JSLint on your projects.

Make a Comment