Site icon Treehouse Blog

How to Build an npm Package

What is npm?

npm is a package manager for JavaScript. It manages dependencies for both front-end and back-end projects.

If you’re a company wanting to distribute a way for developers to connect to your service or an open-source developer who wants to share their code or utility, knowing how to distribute your package on npm is a must.

Where to Start

Sign up for an Account

npm requires an account for you to distribute your software. So, head over to the npm sign up page and enter your details to create an account.

Log in on Your Local Machine

Next you want to make sure you’re logged into the command line by using the login command: npm login

Prepare Your Project

I’ve got a project with a single JavaScript file. The code retrieves Treehouse profile information for a Treehouse student username you pass in:

Here’s how you’d use it:

I also have a README.md file in markdown. This includes the basic usage.

Finally, we need a package.json file. We can use npm init to set it up. Everything in brackets () is the default or guessed value by npm. If you hit enter you accept it and that value will be used for the value inserted into the package.json file. This is the output of my responses:

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (treehouse_profile) 
version: (1.0.0) 
description: An npm Package for Node.js to retrieve Treehouse profile information in JSON from the Treehouse API.
entry point: (profile.js) 
test command: 
git repository: (https://github.com/treehouse/treehouse_profile.js.git) 
keywords: treehouse, profile, badges, points
author: Andrew Chalkley
license: (ISC) MIT
About to write to /Users/andrew/Projects/treehouse_profile/package.json:

{
  "name": "treehouse_profile",
  "version": "1.0.0",
  "description": "An npm Package for Node.js to retrieve Treehouse profile information in JSON from the Treehouse API.",
  "main": "profile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/treehouse/treehouse_profile.js.git"
  },
  "keywords": [
    "treehouse",
    "profile",
    "badges",
    "points"
  ],
  "author": "Andrew Chalkley",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/treehouse/treehouse_profile.js/issues"
  },
  "homepage": "https://github.com/treehouse/treehouse_profile.js#readme"
}


Is this ok? (yes) y

I entered custom values for description, keywords, author and license.

I picked MIT since that’s pretty common in open-source projects like jQuery and Ruby and Rails.

I’m going to commit this package.json and push it up to github.

Testing Package

You can now test the package before you publish it to the npm repository. The npm install command has a neat feature where you can pass in a github username and project and install it directly from that. To install this to test it out locally, in a new project folder, all I need to do is:

npm install treehouse/treehouse_profile.js

Now let’s see if I the example usage works. I just copied the code from the README and put it in an example.js file and ran it with Node.js.

node example.js

And here’s a condensed version of the output:

It works!

Publish!

Now that’s it’s been tested now we can publish it!

Go back into your original package’s folder and type npm publish. You’ll see something like this:

+ treehouse_profile@1.0.0

Now it’s published on npm. You can see it live here: npmjs.com/package/treehouse_profile

Now anyone can do npm install treehouse_profile --save and save it as a dependency for their projects!

Conclusion

npm is one of the easiest ways to distribute software packages. Why not share your code and talents with the world and distribute your software too!?

If you’re new to npm, my npm Basics course is due for release sometime in August 2015 that covers the basic usage.

Exit mobile version