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.

npm-boilerplate

View on GitHub     View on NPM

Create your own NPM Package

Just clone or fork it and off you go!

$ git clone git@github.com:sdeering/npm-boilerplate.git
$ mv npm-boilerplate <your-npm-package-name>
$ cd <your-npm-package-name>

Features

  • 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;
        done();
    });

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

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

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

    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>' );
        done();
    });

});

Then run the unit tests.

$ npm test

npm-boilerplate-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 Readme.md file.

Build Status

[![Build Status](https://secure.travis-ci.org/sdeering/npm-boilerplate.png)](http://travis-ci.org/sdeering/npm-boilerplate)

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 init.author.name "Your Name"
npm set init.author.email "[email protected]"
npm set init.author.url "http://yourblog.com"
email = you@example.com

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.