Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making a Node.js Project into an NPM Package

DZone's Guide to

Making a Node.js Project into an NPM Package

After completing development, while trying to publish an NPM package folder to Backand AWS S3 bucket, we encountered a few salient holes in the vast amount of guides and articles. Read on to see instructions that will hopefully make your life easier when trying to publish an NPM package

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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, initialize it with:

npm init

We found two important points worthy of mentioning.

List the Files

          "files": [
            "backand_sync_s3.js",
            "config.js"
          ],

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:

          "bin": {
            "backand_sync_s3": "./backand_sync_s3.js"
          },

Testing the Package

  • 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

    • Go to another folder, say myApp. Make sure all dependencies of your package were included in its package.json, by removing the node_modules folder.
    • Install your package:
      npm install ~/backand-hosting-s3/backand-hosting-s3-1.0.0.tgz
    • Write a short program that will test your package. In our case, the following gulp file, will do:
       var gulp = require('gulp');
       var backandSync = require('backand-hosting-s3');
       gulp.task('clean', function() {
           return backandSync.clean();
       });

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

    npm install gulp

    And, test the usage of the package with,

    node_modules/gulp/bin/gulp.js 

    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

    Here, we give you the crucial instructions, taken from the NPM guide.

    • Commit your code to GitHub
    • Register at NPM, and obtain a username and password
    • In the package folder, login to NPM,npm login
    • Publish the package:
      npm publish
    • To update the package:
      npm version <update_type>

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

    • Commit your code to GitHub, as the previous instruction modified the version number in package.json
    • Publish the updated package
      npm publish

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

    What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

    Topics:
    node ,npm ,web design and web development

    Published at DZone with permission of

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}