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.

Create The Dependency To Link To

The first step is to ask npm to make the dependency available via this mechanism. In my case, I’m using a module called bluemix-helper-config which is a tool for running applications both locally and on Bluemix without making a lot of config changes – very useful! However I needed to amend it a bit while I was using it, so I exposed it as a link by running npm link from the directory of this dependency.

Npm shows where it has linked to – basically it becomes a globally available 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 :)

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.