Wednesday, June 12, 2013

Getting Started With Node.js Modules and npm (for developers)

In spite of node.js' wild popularity, finding good informative docs and tutorials as to how to develop and distribute apps for the platform can be a chore. Google searches on the matter tend to return either out-of-date or end-user results. So, as a service to the community and for my own edification, I'm going to show you how to get started developing node.js and packaging with npm.

This tutorial assumes you have node.js version 0.8.1+ as well as npm version 1.2.18+ properly installed. It further assumes working on a POSIX-ish environment. Also assumed is that the reader has at least passing knowledge of JavaScript.


"Hello Google"

In order to better illustrate the steps, we are going to develop a super trivial app that will send a search query to google, retrieve the results in json format and then print the results to the screen. So, let's get moving:

1. Let's create a directory and cd into it:

   $ mkdir hello-google && cd $_

2. Now the crucial step to get your node module started:

   $ npm init

This will create a file named package.json which should look something like this:

{
  "name": "hello-google",
  "version": "0.0.1",
  "description": "A trivial app to illustrate node.js dev",
  "main": "main.js",
  "scripts": {
    "test": ""
  },
  "dependencies": {
    },
  "repository": "",
  "author": "Ruben Orduz",
  "license": "MIT"
}

3. Install request module (easier to use than the default http client):

   $ npm install request

4. Let's add request to the list of dependencies in package.json:

{
  "name": "hello-google",
  "version": "0.0.1",
  "description": "A trivial app to illustrate node.js dev",
  "main": "main.js",
  "scripts": {
    "test": ""
  },
  "dependencies": {
    "request": ""
    },
  "repository": "",
  "author": "Ruben Orduz",
  "license": "MIT"
}

5. Let's create main.js and write some code (you can use your text editor of choice in place of 'open'):

   $ touch main.js && open $_

6. Now copy and paste the following code in main.js:

7. Now, let's test everything is working (assuming you are inside hello-google directory):

   $ cd ..
   $ node hello-google/

In a few seconds, you should see something along the lines of:

   PRISM (surveillanc -> http://en.wikipedia.org/w

   Nine Companies Tied to http://www.usnews.com/new

   What Is PRISM? - G -> http://gizmodo.com/what-i

   NSA slides explain the http://www.washingtonpost

8. If you have never submitted any packages to npm before, you must register with the following command:

   $ npm adduser

it will prompt you some questions and within seconds you will have your user and machine authorized to publish to npm's public servers. 

9. Once you are 100% ready that you want to publish a module for public consumption, you can run the following command:

   $ npm publish

10. To make sure your module can be installed and run:

   $ npm install hello-google

11. Let's test it through node:

   $ echo "r = require('hello-google');" > test.js && node test.js

Note that the name the module will be published as it's not the name of the directory, it's the name declared in the package.json

No comments:

Post a Comment