Back to blog home page

Making a Node.js Project into an NPM Package

Posted by on Jan 05, 2016

We recently published an NPM package, backand-hosting-s3, intended to sync a local project folder to Backand AWS S3 bucket. This bucket serves our users as a static web site for their AngularJS projects. The package is to be used in GulpJS files and in the forthcoming Backand CLI (more on this soon).
After completing development, while trying to publish the package, we encountered a few salient holes in the vast amount of guides and articles.

Below we provide some instructions on these points that will hopefully make your life easier when trying to publish an NPM package.

Structuring Package.json

After initialise it with:

We found two important points worthy of mentioning.

List the Files

Define the bin File

The bin file is the entry point to the package that will be put in node_modules/.bin folder when the package is installed. Enabled usage of the package in a command-line mode.

Specify it with:

Testing the Package

  1. In the project folder, do:

This will generate a tgz file within the project folder. In our case, backand-hosting-s3-1.0.0.tgz

  1. Go to another folder, say myApp. Make sure all dependencies of your package were including in its package.json, by removing the node_modules folder.
  2. Install your package:
  3. Write a short program that will test your package. In our case, the following gulp file, will do:

Install gulp, (if not installed globally on your system),

And test the usage of the package with,

If any of your dependencies were not included in your package.json you will get an error and the program will crash.

Publishing the package

We give you here the crucial instructions, taken from the NPM guide.

  1. Commit your code to GitHub
  2. Register at NPM, and obtain a username and password
  3. In the package folder, login to NPM,npm login
  4. Publish the package
  5. To update the package,

where update_type is one of the semantic versioning release types, patch, minor, or major

  1. Commit your code to GitHub, as the previous instruction modified the version number in package.json
  2. Publish the updated package

If you haven’t already, don’t forget to sign up for a Free-Forever Account with Backand.