Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 986 of the free egghead.io lessons, plus get Node.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Using npm link to use node modules that are "in progress"

3:07 Node.js lesson by

It is some times convenient, even necessary, to make use of a module that you are working on before it has been published to the node package manager (npm). The npm link command makes this simple.


egghead.io comment guidelines

Avatar
egghead.io

It is some times convenient, even necessary, to make use of a module that you are working on before it has been published to the node package manager (npm). The npm link command makes this simple.

Avatar
David

Is there a way to get a list of all things you've linked via

npm link so you can then npm unlink those, or at least get a report/status of dependencies you're waiting on to be published??

In reply to egghead.io
Avatar
Tim

Since a linked package is just a symbolically linked folder you run bash scripts in the node_modules folder to list folders of that type only. http://stackoverflow.com/questions/24933955/easy-way-to-list-node-modules-i-have-npm-linked

In reply to David

Sometimes you need to use a module before it has been published, or before you are ready to publish a new version of it. NPM provides a command called "link" which is useful for exactly that purpose. In this case, we have this module named "upper" which has the amazing functionality of taking in a string and returning the uppercase version of it. That's located here in this upper directory, and we want to use it from this user directory.

Our index file and user just requires upper, and then logs out, passing the string "Hello World" to it. We're in the user directory here, so if we try and use this, we're going to get this error that says, "Cannot find module 'upper.'" That's because we haven't installed it. In fact, we really can't install it because upper hasn't been published anywhere.

But there's a way around this, and that is to go over to the upper directory and then say, "npm link," and npm link you can see is going to create a symbolic link in user/local/lib/node modules/upper to that directory that we were just in. We've now created that sym link in our system, so if we go back into the user directory, we now have to basically tell it we want it to use this location.

We're going to say, "npm link upper," and you can see it's going to then create a three-step symbolic link. It's going to link the node modules/upper directory within our user directory to that place that we created the symbolic link in the last step, which then of course points to Upper itself. Now we can actually run our index file that uses Upper, and we see that output of "Hello World."

One thing to note, especially if you're on a Mac, is that when you're done needing this, your module has been published, or you're just done with this project, whatever the case may be, you should unlink this module, because it can cause problems with things like Time Machine that it's sort of hard to track down. If we then just say, "npm unlink upper," you'll see that it unbuilds that reference, and now if we run our program again, we're going to get that error again.

Use npm link with no arguments in the module directory that you want to provide to other modules, and then npm link with that module name where you want to use it. One thing to note is that the npm link command does require that you have the package.json file, because it's going to actually use the name field from there to create that reference.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?