Last Updated: February 25, 2016
·
1.293K
· MidnightLightning

Working with NPM

NPM is the NodeJS Package Manager. Most users will just use it to install dependent packages via the npm install command. However, if you want to submit your packages as a developer, there's a bit more to know:

Setup

First create an account, by going to https://npmjs.org/signup and signing up. Then:

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://coderwall.com/you"

npm adduser

That will create an ~/.npmrc file with lines like:

init.author.name = Your Name
init.author.email = you@example.com
init.author.url = http://coderwall.com/you

Test

Say you've created the amazing-library package, and you want to make sure that amazing-application that uses said package actually works, but is a separate repository. Instead of alternately publishing and version-incrementing each of those two multiple times, there's the npm link command that lets a local folder/repository act like it's the published module.

Go to the amazing-library folder and run npm link. That will create symbolic link in your local npm install's list of globally-installed modules to the current folder. Then go to the amazing-application folder and run npm link amazing-library. That will create a symbolic link in amazing-application's node_modules folder to the global module location (which is pointing at the local folder, so bounces to there).

Publish

The command npm publish sends the package information to NPM, so it can be required by other packages.

However, NPM doesn't scan the git repository for updates, so in order for the world to see any updates you make, you need to re-publish the package. The npm version command makes that slightly easier if you're publishing a git-controlled package by auto-editing the package.json file and setting a git tag at that location.

npm version patch -m "Squashing bugs 42-44"
npm publish

3 Responses
Add your response

That's a very nice tip...
I come from PHP's world and i thought that http://getcomposer.org/ was a nice package manager but npm is also very nice too...

over 1 year ago ·

Nice! I almost forgot about npm version, thanks for sharing :-)

over 1 year ago ·

@nbpalomino: Composer is great for PHP, and when working on PHP projects I do use it (like when working with Silex), though NPM is only for working on Node projects. I haven't used Composer as a package submitter yet, so not quite sure how that compares to NPM from that perspective!

over 1 year ago ·