How to npm link to node modules under development

Node Package Manager

If you are node developer, many a times you may end up working with modules that depends on other module(s), which are yet to published or need to be updated. And often times in order to publish your current module it is necessary to ensure that, the modules that it depends on works fine with appropriate versioning. Sometimes this gonna be a problem which needs a good workflow which I am going to discuss in this post.

The following are the three possible solutions,

Local npm install:


npm install ../path/to/module 

Pros: Easy to setup.

Cons: This method needs the module to be re-installed whenever theres a change in the module.

Using Relative paths:


var mod = require(‘../../path/to/module');

Pros: Easy to setup. Okay for development even though its not recommended.

Cons: Not production ready code. This method needs a code change before the module gets published.

Using npm link:


npm link

Step 1: Go to the root folder of the module that you want to refer from somewhere else and run

npm link

Step 2: Go to the target module’s root folder where you need refer the module from step 1 and run

npm link module-name 

Pros: Easy coding, publishing and maintenance. Recommended setup for development.

Cons: Setting up can be confusing and complicated if there are many inter-dependent modules, as the linking chain needs to be done from bottom to top. Say if app depends on module 1 which in turn depends on module 2 then, module 2 needs to be linked with module 1 first before linking module 1 with app.

Linking modules from bottom to top app <- module1 <- module2

If you find all the above steps very complicated, then don’t worry. You have got npm-link-shared for the rescue.

Leave a Comment

Your email address will not be published. Required fields are marked *