Working with unpublished node dependencies

27-Feb-2019 • 3 min read

Alright! If you are a Node.js developer, you definitely have ended up in a situation where you want use an unfinished feature from another node dependency.

Let's elaborate this a bit. For example your entire project is broken logically into 4 npm modules. One module, which is the main one depends on other 3 modules. With this setup, you might have to change the code in sub-modules and check if it works well with your main node module.

Simplest way is to publish the modules to npm. Use the new versions in your main node module. Well, the downside with this approach is if you have made a mistake in your sub modules, you have to re-publish and use them accordingly. But, things don't stop there. You will have to repeat this until your main node module is stable. Headache. Right? I know.

So, How do we get around this problem?

Using npm link

With this approach you can work with any node dependencies if they are checked out at some location in your local machine. All you have to do is run below command in the root folder of the package, which is a dependency for your main node module.

npm link

Alright! What does this do? If you have worked on node based projects, you know there is node_modules folder which has your installed dependencies. Similarly there is a global folder for the dependencies. Above command creates a symbolic link for the package in which this command is run. You have to run this command again in the package where you want to use the dependency code with the name in package.json.

npm link <package-name>

With this, any changes you make to your dependency node module will be usable directly without having to re-install. Above 2 steps can be made short with just below command.

npm link <relative-path-to-the-dependency>
Getting the source from github

Now, let's discuss another use case where you are not the one working on your dependency, but a colleague of yours. And he don't want to publish the code until he makes sure the feature is complete to some extent. But you need this person's code to test any early stage integration issues. I am assuming you both use git version control system for managing your code. You can get the changes your colleague has pushed to git with the link to the repository code as below in your package.json file.

package.json

'package-name': git@github.com:<repository-name>.git#<branch-name>

Once you placed above path in package.json file, you need to run a clean npm install to get the latest code from git.

Hope you enjoyed the article. If you like this article please do like and share with others.

Comment down below if you've another way of working with node dependencies.