Skip to content

Create and publish an npm package

April 10, 2015Maxime Thoonsen3 min read

I just made an npm package: gulp-backpack and it was pretty easy to publish it to npm! Therefore, I wanted to share what you need to know so you can do it too.

Create your package

Obviously before publishing anything you'll need to code it!

Package.json

The core of your node package is the package.json file. If there is one file you should care about, it's this one. There is nice documentation about what you must and what you can do with this file.

Here are the most important aspects of the package.json file:

  • The name and the version of your package are required to allow people to install the package
  • Add the dependencies needed for your package.
  • Add some entries so npm users will have a better understanding of your package like description, license, author, homepage, keywords, repository
  • Scripts section should contain the tests of your application because anyone should be able to run them with a npm test.

Require your module

When you want to use your module in a js program, you require it. You do something like

gbp = require 'gulp-backpack'
gbp.gulp.task 'clean', ->
  gbp.del.sync 'www'

You may wonder which file will be called when you require the module. The rule is simple:

It will run the *index.js* in the root directory of your module
Unless you specify a *main* entry in your package.json

By the way, have you ever asked yourself wtf is going on when you require a module?

Write or at least build your module in JS

You may code in CoffeeScript but don't forget that it's not everyone's case. As you go open source, you want to target as many people as you can!

Test your module

Build Status

When you are coding for open source, you can use a lot of nice tools for free.

For testing purposes, you can use Travis to run your tests and Coveralls for your code coverage. Add a .travis.yml file to tell Travis what it has to do.

If you don't know how to configure Travis have a look at Reynald's tutorial.
For a node package, it's pretty simple because Travis will automatically run npm install and npm test.

You can easily tell Travis with which version of node you want the tests to be run:

node_js:
  - "0.10"
  - "0.12"
  - "iojs"
after_script:
  - npm run coveralls

Building a CLI tool

If you are willing to create a CLI tool. I advise you to use Liftoff, it will help you a lot!

Publishing

As the official documentation is not so explicit, I had to search a little bit. I finally found this excellent Gist written by AJ ONeal. Thanks to him I was able to follow these simple steps to publish my package.

Create an npm user

You need to create a user on the npm's platform. Use the npm command for that, no need to sign up on the website. First, you have to configure your author information so that npm can create your account.

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://yourblog.com"

Then you are good to create your user. Use the npm command line as well.

npm adduser

It will ask your three questions...

The username you want to use for your npm's account
The password of your npm's account.
The email you will use for your npm's account. It will be publicly shown on the npm's website

...and it's done! Don't worry if there is no confirmation message.
Just go to https://www.npmjs.com/~ to check that your account has been created.

Publish

This part is for very skilled developers because you have to run:

npm publish

and you are done. You can search your package: it should already be available.

gulp-backpack

backpack 29635 640

A little more about gulp-backpack. As I said in the README:

This repository is meant to simplify the inclusion of all those tools and gulp plugins we all need to compile our angular apps with using gulp. One gulp-backpack to gulp them all!

I will enjoy any feedback about it!