May 11, 2020 // Tutorial

Publish a Package to NPM

#npm #javascript

Publishing a package to NPM is a quick and efficient way to re-use code across multiple projects.

Prerequisites

Before we get going, make sure you have an NPM account and have NPM installed and updated by running npm i -g [email protected]. You'll also need Node installed.

Create a Project

Let's get a project up and running. We'll build a small utility to catch and help us fix all our errors.

  1. Create an empty directory: mkdir super-error-handler && cd super-error-handler
  2. Generate a package.json: npm init -y This will produce some fine defaults.
  3. Create your utility: touch index.js
// super-error-handler/index.js

module.exports = function handleError(error) {
  const query = encodeURI(error.message)
  console.error(`https://stackoverflow.com/search?q=${query}`)
}

Create a Test Environment

Now that we have some code, let's see if it works!

  1. Create another empty directory outside of the original: mkdir test-env && cd test-env
  2. Generate another package.json: npm init -y
  3. Create a symbolic link to your utility so you can require it like other installed modules: npm link /path/to/super-error-handler
  4. Create a file to test with: touch index.js
// test-env/index.js

const handleError = require('super-error-handler')

function testErrorHandler() {
  try {
    throw new Error('something bad happened')
  } catch (error) {
    handleError(error)
  }
}

testErrorHandler()

Now that we have some test code written with our error handler, let's give it a try: run node index.js.

Publish to NPM

Almost there! Since every NPM package needs a unique name, you'll need to namespace your package by setting the name field in your package.json to @your_npm_username/super-error-handler.

Finally, from the super-error-handler directory, run npm adduser to authenticate to NPM and then npm publish.

That's it! You've published your first NPM package! Head to npmjs.org to search for it and check it out.

Additional Tips

Now that your code is available on NPM, here are a few things you can and should do:

  • Check out the NPM docs on package.json fields and fill them in appropriately.
  • Create a GitHub repo for your project.
  • Add an .npmignore file to keep some of your repo from being downloaded by users of your module. Here's a great Stack Overflow question detailing how you might use one.
  • Use np to level up your NPM game. It helps to take care of a bunch of additional publishing steps, including creating a release on GitHub. Read the docs on NPM.

Need some help or want to share your newly-uploaded NPM package? Feel free to reach out.