NPM Boilerplate – A Good Starting Point

NPM Boilerplate – A good starting point for your new Node.js NPM packages. A skeleton project which has everything you need for creating a new NPM package. I’m looking to enhance this template so all forks and PR’s much appreciated.


View on GitHub     View on NPM

Create your own NPM Package

Just clone or fork it and off you go!

$ git clone
$ mv npm-boilerplate <your-npm-package-name>
$ cd <your-npm-package-name>


  • Package.json – NPM policy requirements
  • Module.js – NPM module
  • Utils.js – Module utils
  • Testing – Run unit tests using Mocha, CI tests using Travis

Unit Testing

Testing is provided by MOCHA. Either run in bash `$ npm test` or the full command `$ ./node_modules/.bin/mocha –reporter spec`.

Edit tests in `test/module.test.js`. Here are a few sample tests which check the module is created, tests the config, initialize, api functions and module utils.

  Use `npm test` to run tests using mocha.
  or `./node_modules/.bin/mocha --reporter spec`

var path = require( 'path' )
  , MyModule = require( path.resolve( path.join( __dirname, '..' ) ) )
  , expect = require( 'chai' ).expect;

describe('my module', function( done ) {

    it('should load as a module', function(done) {
        expect( MyModule ).to.exist;

    it('should set default config values', function( done ) {
        var config = MyModule.getConfig();
        expect( config.defaultValue1 ).to.equal( true );

    it('should override the default config values', function( done ) {
            defaultValue1: false
        var config = MyModule.getConfig();
        expect( config.defaultValue1 ).to.equal(false);
        expect( config.defaultValue2 ).to.equal(true);

    it('should run public api functions', function( done ) {
        var results = MyModule.multiply( 2 );
        expect( results ).to.deep.equal( [ 2, 4, 6 ] );

    it('should run module utils functions', function( done ) {
        var html = MyModule.stripHTML( '<div class="my-class">some junk ãÅäÆ</div>' );
        expect( html ).to.equal( '<div class="my-class">some junk </div>' );


Then run the unit tests.

$ npm test


CI Testing

Hook it up to Travis CI.

Then your tests will run automatically when you push to the repo.

23-04-2014 10-44-37 AM

Add the Travis image to your repo file.

Build Status

[![Build Status](](

Then follow this Gist which details how to add a user to npm and publish your package.

Add this to your ~/.npmrc file.

npm set "Your Name"
npm set "[email protected]"
npm set ""
email =

Type into bash adduser and enter your npm username and password.

npm adduser

Then check your project for errors, missing dependencies etc before you publish.

cd /path/to/your-project
npm init

npm install -g pakmanager
# this shows you dependencies as you `require`d them
pakmanager deps

# now edit `package.json` and add any deps you forgot about
npm publish ./

That’s it! Your package should now appear on NPM.