Sometimes javascript is developed and tested by pushing it to the website and then checking if it is working in browser as intended. If your code base is large then pushing it to a development server can be time consuming. Using JSLint on command line, we can find many errors in the javascript without the need of a web browser.
JSLint Introduction
Here is the definition of JSLint as per Wikipedia JSLint page:
JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It was developed by Douglas Crockford.
You can get detailed information about jslint on the JSLint official site. It is also interesting to note that JSLint itself is written in Javascript. Its source code can be accessed from jslint github page.
Installing Jslint on Ubuntu Linux
We’ll be using the node.js version of JSLint (node-jslint) for running jslint on command line. Here are the steps to install it on Ubuntu Linux:
- First install nodejs and npm on Ubuntu Linux.
- Using npm, install jslint using the following command:
sudo npm install -g jslint
Note that
-g
is for global install which installs jslint in/usr/local/lib/node_modules/
. - Now run jslint on command line without any argument. You should see the help message.
No files specified. Usage: /usr/local/bin/jslint [--indent] [--maxerr] [--maxlen] [--predef] [--anon] [--bitwise] [--browser] [--cap] [--continue] [--css] [--debug] [--devel] [--eqeq] [--es5] [--evil] [--forin] [--fragment] [--newcap] [--node] [--nomen] [--on] [--passfail] [--plusplus] [--properties] [--regexp] [--rhino] [--undef] [--unparam] [--sloppy] [--stupid] [--sub] [--vars] [--white] [--widget] [--windows] [--json] [--color] [--terse] [--] <scriptfile>...
Using JSLint
JSLint can be used on one or more javascript files. Here are some notes on how to use JSLint on command line:
- To run jslint on foo.js, run this command:
jslint foo.js
- If you get “Missing ‘use strict’ statement.” error message, then add the following line to your code:
"use strict";
Note that this line is a backward compatible line and does not cause any problem even is the some browser javascript interpreter does not recognize it. It is just treated as a harmless string in that case.
- If you are using a global variable (e.g. jQuery), you may get this eror:
'jQuery' was used before it was defined.
To fix this add
/*global jQuery */
somewhere at the top of your js file. - JSLint assumes an indentation of 4 spaces. In case you are using a 2 space indent, run jslint with –indent 2. Here is how it will look like:
jslint --indent 2 foo.js
or alternatively you can add
/*jslint indent: 2 */
line at the top of your file. - In case you are using a bash script to push your code to development or production server, you can use
set -e
and do a jslint before the code push line. That way if there is a jslint error, the script will exit instead of continuing. Here is how the bash script code will look like:#!/bin/bash set -e jslint foo.js ## Commands to push code to server ...
Using JSLint on command line can help you detect many javascript errors much faster and thus lead to an improved workflow. It also keep javascript code lot more readable and hence more maintainable.