Installing a local module using npm?

Installing a local module using npm?

I have a downloaded module repo, I want to install it locally, not globally in another directory?
What is an easy way to do this?


Solution 1:

From the npm-link documentation:

In the local module directory:

$ cd ./package-dir
$ npm link

In the directory of the project to use the module:

$ cd ./project-dir
$ npm link package-name

Or in one go using relative paths:

$ cd ./project-dir
$ npm link ../package-dir

This is equivalent to using two commands above under the hood.

Solution 2:

you just provide one <folder> argument to npm install, argument should point toward the local folder instead of the package name:

npm install /path

Solution 3:

Since asked and answered by the same person, I’ll add a npm link as an alternative.

from docs:

This is handy for installing your own stuff, so that you can work on it and test it iteratively without having to continually rebuild.

cd ~/projects/node-bloggy  # go into the dir of your main project
npm link ../node-redis     # link the dir of your dependency

[Edit] As of NPM 2.0, you can declare local dependencies in package.json

"dependencies": {
    "bar": "file:../foo/bar"

Solution 4:

Neither of these approaches (npm link or package.json file dependency) work if the local module has peer dependencies that you only want to install in your project’s scope.

For example:

  "name": "mymodule",
    "foo": "^2.5"

    "mymodule": "file:/local/mymodule",
    "foo": "^2.5"

In this scenario, npm sets up myproject‘s node_modules/ like this:

  mymodule -> /local/mymodule

When node loads mymodule and it does require('foo'), node resolves the mymodule symlink, and then only looks in /local/mymodule/node_modules/ (and its ancestors) for foo, which it doen’t find. Instead, we want node to look in /local/myproject/node_modules/, since that’s where were running our project from, and where foo is installed.

So, we either need a way to tell node to not resolve this symlink when looking for foo, or we need a way to tell npm to install a copy of mymodule when the file dependency syntax is used in package.json. I haven’t found a way to do either, unfortunately 🙁