This website recently got a major rebuild; if you're missing something, let Lorna know.

Using NPM Link to Develop Dependent Projects



Right now I'm working on a javascript project that relies on another module for some of its functionality. I'm making fairly major changes that affect both projects, and since the dependency is pulled in via npm, initially I was committing and pushing to a repo so that npm could pull in the dependency from GitHub - on every update. Well that gets tedious really quickly so I'm now using the npm link command which is pretty handy. I hadn't seen it before so this blog post is basically the cheat sheet I wrote when I started using it...

The npm link command essentially creates a symlink in the node_modules folder so that any edits you make in one module will immediately be reflected in any project that has been set up to depend on that (local) version of the module.

Use The Linked Version Of The Module

In my main project (I was setting up a simple-data-pipe to bring in StackOverflow data), I can then link to this module by doing:

npm link bluemix-helper-config

Npm gives some output showing the actual directory it's linked to, the global location, and where that is in my local node_modules folder. You can also see the effect of an npm link setup by listing the contents of the directory - an ls -la command shows where the link goes to.

Beyond The Development Platform

The link is only used locally, if you're pushing to the cloud you'll still need to publish your dependencies to somewhere that they can be loaded from, even if you set up your package.json to track a branch by doing something like:

"bluemix-helper-config": "git://github.com/lornajane/bluemix-helper-config.git#remove-vcap-app-host",

I found that this approach improves my development workflow a bit when I find that dependencies need some updates as well as whatever I'm doing - and that using the branch notation means I can ship with my fixes immediately, then update back to a proper version number once a new release is available. If there's something you'd add here, I'd love to hear it - I'm relatively new to nodejs so there's probably some other tips I haven't learned yet :)


In: javascript
Tags: #nodejs #npm #tech