Be the first user to complete this post
|Add to List|
How to publish a package on npm
So, you are ready to publish a package to npm but are not sure how to go about it? This blog post aims to provide one approach to publishing packages onto npm. So, read on, and if something doenst chime, let me know in the comments.
PrerequisitesYou have already creaed an account on https://www.npmjs.com/ and can login via the command line using
npm adduser- https://docs.npmjs.com/cli/adduser.
Step 1: Local verificationVerify that your package installs locally from another folder.
You can now use the node command line to require and test your package node
cd .. mkdir test-mypackage cd test-mypackage npm i ../test-mypackage
To see what gets published to npm, create a tarball in your package directory using
var mypackage = require('mypackage');
To list the contents of the generated tar file
cd mypackage npm pack
By default npm packages the current directory. So be mindful of the branch that is checked out.
tar -ztvf mypackage.tgz
Step 2: Managing the publish processLets assume you are working on
mypackagewhose current stable version in the master branch, as specified in package.json is
1.2.0. Your have some new features ready go to into 1.3.0, but you're not sure how stable your changes are. Here's one way of thinking about the release process. - By default, all existing users should continue to get
npm install. - Certain users should receive the next version, which in this case is 1.3.x, of the project, but they should not have to speficy the exact version all the time. You can achieve this by using dist tags when publishing a package.
Think of dist tags as channels to which users can subscribe.When you publish your package simply using
npm publishcommand its gets published and assigned to the
latesttag. As a consumer of a package, it implies that when I type
npm install mypackage, npm looks at the registry and tries to find the published version that is tagged with the
latesttag, and installs that version. So if you want beta/test users to install a new version release, all you gotta do is tag associate it with a different tag. e.g. In our case, here's what we can do Checkout a new branch and name it based on your release. E.g.
In this branch update the version number in your package.json
git checkout -b release/1.3.0
This automatically updates the version in your
npm version 1.3.0-rc.1
package.jsonand commits. It also creates a git tag for you.
As per the docs, do not begin your tags with the characterYou can list all the available git tags using the command
Now your goal is to publish the package on npm, and assign it a dist-tag. Several projects use the convention of a
nexttag to denote upcoming releases, so we will follow that same convention.
This will take the current directory (hence the current checked out branch), run build scripts if any, and publish it to npm. It will also tag it with a label called
npm publish --tag next
next. Now, your users can install the package using
This way, in the future, when you have another release candidate for 1.3, lets call it
npm install [email protected] OR npm install [email protected]
1.3.0-rc.2, you can run the following commands
And as long as your users are using
npm version 1.3.0-rc.2 npm publish --tag next
[email protected], they will automatically get the changes in
1.3.0-rc.2without having to specify the exact number. If they wish to, they can still explicitly choose to install a particular version using
npm install [email protected]or
npm install [email protected]
BONUS There is also a package semantic-release that you could look at if you need something more automated.I hope you find this flow useful. If you feel there are ways I could improve this, I will be happy to learn.
- Installing, Listing and Uninstalling packages using npm
- What is an npmignore file and what is it used for
- exports is not defined
- Creating a simple event emitter using nodejs
- What is npm shrinkwrap and when is it needed
- Accessing the request body and session in nodejs using express