In the previous article we talked about how to create, publish and use your private NPM packages.
This time is the moment to talk about how to simultaneously work on an application and a dependency used by that application.
When you create your own public or private NPM packages is basically because you want to use that package as a dependency in another application.
However if you need to perform some changes on that dependency and test those new features or bugfixes in the application, how can you test them without publish a new version of that dependency and change the manifest of your application ? How to avoid all that polution in your repos ?
NPM link is the answer.
Of course there’re always hand made solutions, you could make changes directly in the node_modules folder and manually copy the changes to the git repository of the dependency once you are done.
This is a quick & dirty, time consuming and prone to errors solution. If you use
npm link you will realize how much cleaner is.
How NPM link works
This package linking process consists in two steps process. In short, create a symlink (symbolic link) and tell your application to use that link. Let’s see how this works:
1. Create the global symlink for your dependency package using
npm link command:
cd ~/projects/npm-dependency/ npm link
2. Now your application needs to use that symlink, using
npm link command:
cd ~/projects/my-application/ npm link npm-dependency
It’s just that simple !
Now you can work on your
npm-dependency as usual, edit, compile, run tests, etc. At any change you make on
my-application it will take all those changes immediately, helping you out to test that your dependency works as expected inside the application.
Undoing your links
Once you have completely finished, it’s time to tell your application to get back to normal.
Getting back to use the npm-depdency version is as simple as deleting the symlinks you just have created. In this case the process should be undone in two steps process as before.
1. Tell your application to stop using the symlink:
cd ~/projects/my-application/ npm uninstall --no-save npm-dependency && npm install
2. Remove the global symlink for the dependency:
cd ~/projects/npm-dependency/ npm uninstall
More about NPM link
I recommend you to have a look at the official documentation about npm link here: