HTML and CSS Reference
In-Depth Information
If npm isn't installed, you can still install it by following the instructions at http:///npmjs.org/ . But first check
that your Node install is up-to-date.
Installing Modules
By default, npm installs packages locally into a directory called node_modules under your current directory.
When you build a server-side app, this makes a lot of sense so you can control exactly which versions of libraries
are used. When you use Node from the command line, however, you often want to install certain modules glob-
ally, so the binaries are available wherever you are.
To install a module globally, use the --global option. One module particularly useful is the jshint module.
This module, a slightly less opinionated derivative of Douglas Crockford's JSLint tool, parses your JavaScript
code and gives you feedback about what parts need to be tuned up.
To install this module, run the following command from the command line:
npm install --global jshint
This installs the jshint module and makes the binary accessible from the command line.
Hinting Your Code
With the jshint node module installed, you can now run jshint from the command line to run a quick syntax
check on your code. JSHint can discover errors such as missing semi-colons or odd structures that wouldn't
necessarily prevent your code from running, but might lead to tough-to-find bugs down the road.
To run jshint on a file, run the command at the command line followed by the name of the file. For ex-
ample:
jshint engine.js
JSHint generates a list of descriptive warnings, including line and column numbers, which can help you im-
prove the JavaScript you write.
Uglifying Your Code
When you deploy your game for players to play, you want it to load as quickly as possible. One way to cut down
on the load time is to keep the size of what you transfer over the wire as small as possible. Run your JavaScript
code through one of the many JavaScript minifiers that have been written to reduce file size.
JavaScript minifiers take your JavaScript, remove the whitespace, and then rewrite and shorten local vari-
ables and parameters to significantly reduce the size of the code. Using the Alien Invasion game from Chapter 3,
“Finishing Up and Going Mobile,” running engine.js and game.js through the uglify-js minifier reduces
the size of the code from almost 19 K to just more than 11 K, a compression of more than 40%.
To install uglify-js, install the module via npm globally:
npm install --global uglify-js
The uglify-js binary takes only one file, so if you want to merge multiple files into one (as you should), you'll
need to concatenate them separately. On Windows you can run the following:
type file1.js file2.js > all.js
uglify-js all.js > all.min.js
Search WWH ::




Custom Search