If you are a Node.js developer, you might've ended up in a situation where you want to 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 the other 3 modules. With this setup, let's get changes from sub-modules to integrate with the main module.
The simplest way is to publish the sub-modules to npm
. Use the new versions in your main node module's package.json
file and re-install. 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.js projects, you know there is a node_modules
folder that has your installed dependencies. Similarly, there is a global folder for the dependencies. The above command creates a symbolic link for the package in which this command is run in the global folder. 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 used directly without having to re-install. The 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 doesn’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 the 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-name': 'git@github.com:<repository-name>.git#<branch-name>'
Once you've placed the above path in package.json
file, you need to run a clean npm install
to get the latest code from git.
Comment down if you using any other approach?
Top comments (0)