Getting ready

Node.js has its own package manager, Node Packaged Modules (npm), which allows you to install third party modules and their dependencies. Modules are either installed locally, inside a directory, or globally, that is, on a location typically available as part of your path so they can be globally accessed.

Most Node.js applications make extensive use of modules. A module is basically application code described by a package.json file. The code and packages.json files can be in a directory, a Git repository, a compressed tarball, or a URI to a compressed tarball.

A package or module can be published to the npm registry. Once published, it can be referred to by a <name>@<version> string, where <version> can also be a tag or defaults to the latest tag if omitted. A module can be installed by doing the following:

$ npm install [-g] <module reference>

In this case, <module reference> is one of the following:

  • A directory containing the packages.json file and the code itself
  • A compressed tarball of the above, or a URI to it
  • A Git repository with the packages.json file and the code itself
  • A <name>@<version> string that represents a module published in the npm registry

In the previous command, the optional -g is specifying a global install.

To create a package we can, for example, use the following helloworld.js code:

var http = require('http'); 
http.createServer(function (request, response) { 
    response.writeHead(200, {'Content-Type': 'text/plain'}); 
    response.end('Hello World
'); 
}).listen(8080); 
console.log('Server started'); 

You will also need to use its corresponding packages.json description file:

{ 
  "name": "nodejs-helloworld",                                               
  "version": "1.0.0",                                                            
  "description": "An example node.js hello world server",                        
  "main": "helloworld.js",                                                            
  "author": "Alex Gonzalez <[email protected]>",                           
  "license": "ISC"                                                               
} 

The creation of a packages.json file can be automated with the npm tool by doing the following from the same directory:

$ npm init

To try the module out, we can install it globally with the following:

$ npm install -g .

The . in the previous command specifies the current directory. It can be executed with the following:

$ nodejs helloworld.js

Directing a browser to localhost:8080 displays the Hello World message.

Once it works, we can publish it to the npm registry with the following:

$ npm adduser
$ npm publish

Assuming your package name is unique, you should be able to see your package at https://npmjs.com/package/<package>. It can also be published to GitHub, so that it can be directly installed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset