Javascript's Modern Web Tools – Node Package Manager

by
Tags: , , ,
Category:

NPM – the Node Package Manager

The Node Package Manager, or npm, is one of the most useful utilities downloaded with NodeJS. It is used to install various Javascript tools and libraries, including web frameworks like Connect and Express. In this tutorial segment, we’ll show you how to set up tools and libraries, and we’ll end by creating a simple web server with the Node Express web framework using only a handful of lines of code.

Node command-line tools – Linting your code with jslint

One useful utility available via npm is jslint – a linter (syntax checker) for Javascript. let's use npm to install the JSLint linter, globally (adding it to the OS path):

$ npm install -g jslint
(output)
npm http GET https://registry.npmjs.org/jslint
npm http 200 https://registry.npmjs.org/jslint
npm http GET https://registry.npmjs.org/jslint/-/jslint-0.2.6.tgz
npm http 200 https://registry.npmjs.org/jslint/-/jslint-0.2.6.tgz
npm http GET https://registry.npmjs.org/nopt
npm http 304 https://registry.npmjs.org/nopt
npm http GET https://registry.npmjs.org/abbrev
npm http 304 https://registry.npmjs.org/abbrev
/Users/kenrimple/.nvm/v0.10.22/bin/jslint ->
  /Users/kenrimple/.nvm/v0.10.22/lib/node_modules
  /jslint/bin/jslint.js

There, it’s set up. Now, to run it:

$ jslint silly.js
(output)
silly.js
 #1 Expected 'var' at column 1, not column 4.
    var x = 1, y, z; // Line 1, Pos 4
 #2 Expected 'var' at column 1, not column 4.
    var q = 123; // Line 3, Pos 4
 #3 Expected 'console' at column 1, not column 7.
    console.log("give me a break!!!"); // Line 5, Pos 7

Pretty nifty, eh? There are over 50,000 npm modules out there, and there are a ton of useful command-line utilities to be installed. But you don’t install everything with the global (-g) flag, because otherwise you’d have a huge, bloated OS and an impossible to reproduce build environment.

Most Javascript server-side projects in NodeJS are configured using npm. For example, let’s say you want to access a number of Javascript server-side APIs, but you don’t want to check in the script files, just the instructions for fetching them. Project configuration is npm’s key feature.

Let’s set up a simple project. First, configure a project definition using npm init, answering the questions you’ve been given:

$ npm init
(interactive output session)
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See npm help json for definitive documentation on these fields
and exactly what they do.
Use npm install  --save afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (bah) demo-project
version: (0.0.0) 0.0.1
description: Demo Project
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (BSD-2-Clause)
About to write to /Users/kenrimple/bah/package.json:
{
  "name": "demo-project",
  "version": "0.0.1",
  "description": "Demo Project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "BSD-2-Clause"
}

This interactive session creates a package.json configuration file. You interact with it using npm, installing libraries and tools.

Now you can start developing server-side Javascript software using NodeJS.

Let’s use npm to install the Node Express framework, a great MVC platform, in the current project folder. Execute:

$ npm install express --save-dev

The npm tool downloads all of the software to ./node_modules, and adds the following lines to your package.json project file:

"devDependencies": {
   "express": "~3.4.7"
 }

But *only* if you add --save-dev to the end.

NPM and version management

Now your project can be checked in, sans the node_modules directory as npm can install it for other developers later. Pretty nifty, eh? And there are many more utilities out there to manage aspects of your platform.

A simple HTTP server in Express

Now that we’ve installed the Express API to node_modules, let’s write a simple HTTP web server. We’ll configure Express to use a public folder beneath our package.json file, and to serve the files using HTTP:

var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public')));
http.createServer(app).listen(
  app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

And that’s it. Create a directory called public, place an index.html file in that location, and fire up your web browser to http://localhost:3000. To run the server, use:

$ node server.js

Express is a powerful web server. To see what else you can do with it, head over to expressjs.com/guide.html for a great tutorial, including using the express command-line build tool to create a fully-featured MVC platform.

Our next post will feature browser-side dependency management using bower.