Creating a NodeJs module HOWTO
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Fortunes module

NodeJS module HOWTO.

Create the basic structure

Create a directory fortunes-module and put in it the following files:

|-- package.json
|-- bin
|   `-- nfortune
|-- lib
|   `-- main.js
`-- test
    `-- main.js


Setup a local git repository:

git init
git add .
git commit -m "create the basic structure"

Install the github gem:

gem install github

Create the remote repo at GitHub:

github create-from-local

Continous integration with Travis

  • login into Travis with Github account
  • goto the Travis profile page
  • add a .travis.yml file:
language: node_js
  - 0.6
  • add, commit and push changes to GitHub

Build Status

Travis resources

Make the module

Use npm init to create the following package.json file:

  "author": "Włodek Bzyl <>",
  "contributors": [
    { "name": "Włodek Bzyl", "email": "" }
  "name": "fortunes-module",
  "description": "print a random quote",
  "version": "0.0.1",
  "keywords": ["fortune", "template", "module"],
  "repository": {
    "type": "git",
    "url": "git://"
  "main": "lib/main.js",
  "bin": {
    "nfortune": "./bin/nfortune"
  "scripts": {
    "test": "mocha test/*.js"
  "dependencies": {},
  "devDependencies": {
    "mocha": "*",
    "should": "*"
  "optionalDependencies": {},
  "engines": {
    "node": "*"

Install the devDependencies:

npm install

and ignore them:

echo node_modules/ > .gitignore
git add package.json .gitignore
git commit -m 'ignore installed dependencies'

Make the first test

var should = require('should')
var fortunes = require('../lib/main')

describe('fortunes module', function() {
  describe('with no arguments', function() {
    it('returns a random quote', function() {
      var result = fortunes()

Publish the module

Try the module locally:

npm install -g .
npm rm -g fortunes-module

Register in

npm adduser

Change the version to 1.0.0 in package.json commit & push changes to GitHub:

git add .
git commit -m "ready for production"
git tag v1.0.0
git push --tags

And finally publish it:

npm publish .