Over a million developers have joined DZone.

Using npm Link to Develop Dependent Projects

DZone's Guide to

Using npm Link to Develop Dependent Projects

Need to keep two related/dependent projects in sync with their respective changes? Check out how to use npm's link feature to make your life easier!

· Web Dev Zone ·
Free Resource

Building real-time chat? Enroll in a Free Course on Mobile Chat Development. 

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 Node.js so there's probably some other tips I haven't learned yet!

Power realtime chat, IoT and messaging apps at scale. Pubsub realtime messaging, functions, chat, presence, push, notifications, blocks catalog and more.

development ,npm ,dependencies

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}