20 Mar 2015
npm link you can depend on other Node modules without having to rely on a NPM repository.
It also saves you from specifying some complex path to the dependency on your file system.
Using the dependency works as it was resolved through a NPM repository.
Maybe the dependency is still under development or a private NPM repository is overkill for your project?
The Farm application
Imagine we’re creating this ‘farm’ application. It has two files:
As you can see, this application depends on the
farm-cow module. This dependency is not part of our application and is still under development by someone else.
The dependency: farm-cow
This module has two files:
When we try to run the farm application, an error is thrown:
This is normal, because the farm-cow module is not available in the node_modules directory.
npm install won’t solve anything because the
farm-cow module is not available in our NPM repository:
Because we can reach the dependency through the file system, we can use
npm link to resolve it.
Making the module available
Navigate to the dependency in our terminal:
npm link command has created a symlink that points to the farm-cow directory. Note that it used the value of the ‘name’ attribute in the package.json file.
Using the module
Navigate to the module that requires this dependency:
npm link command has now created another symlink to the application that needs the directory.
Running the farm
app.js will work now because the farm-cow module is resolved through the symlink:
We can break the link by using the
npm unlink command in the farm application:
If you liked this post, you can
share it with your followers
follow me on Twitter!
comments powered by