Installing a local module using npm?

362,708

Solution 1

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 2

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 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

npm pack + package.json

This is what worked for me:

STEP 1: In module project, execute npm pack:

This will build a <package-name>-<version>.tar.gz file.

STEP 2: Move the file to the consumer project

Ideally you can put all such files in a tmp folder in your consumer-project root:

STEP 3: Refer it in your package.json:

"dependencies": {
  "my-package": "file:/./tmp/my-package-1.3.3.tar.gz"
}

STEP 4: Install the packages:

npm install or npm i or yarn

Now, your package would be available in your consumer-project's node_modules folder.

Good Luck...

Solution 5

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:

/local/mymodule/package.json:
  "name": "mymodule",
  "peerDependencies":
  {
    "foo": "^2.5"
  }

/dev/myproject/package.json:
  "dependencies":
  {
    "mymodule": "file:/local/mymodule",
    "foo": "^2.5"
  }

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

/dev/myproject/node_modules/
  foo/
  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 :(

Share:
362,708
fancy
Author by

fancy

Updated on February 10, 2022

Comments

  • fancy
    fancy about 2 years

    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?

  • Dusty J
    Dusty J over 10 years
    It might not be original intent of the question, but it's probably what most people who find this through google want.
  • Thomas Potaire
    Thomas Potaire over 9 years
    This answer seems incomplete, you need to run npm link against the folder once (to create a global symlink) and then run npm link package-name within the folder of the project (to use the global symlink in your project). The answer below is the right answer.
  • Michael Allan Jackson
    Michael Allan Jackson over 9 years
    @ThomasPotaire both answers are correct. If you look at the npm link documentation, it presents both methods, with this relative directory approach as a shorthand.
  • smaudet
    smaudet over 8 years
    This is the only sane looking approach I've seen so far - why npm has to be so obscure/obtuse w. regards to creating a local package, installing it and then using it, I don't know... link works, (and its great), but the terminology is rather confusing.
  • Tyler Collier
    Tyler Collier over 8 years
    @Rich Apodaca, thanks for the doc link. It doesn't mention undoing the process. It looks like all it does is create symlinks, so I can remove those as normal?
  • Rich Apodaca
    Rich Apodaca over 7 years
    @TylerCollier npm unlink appears to be the mirror-image operation stackoverflow.com/a/24940024/54426
  • Daniel Waltrip
    Daniel Waltrip over 7 years
    The second method (using the file: approach) allowed for my app and the local module to share a dependency. My test of npm link resulted in a duplicate dependency, which breaks things if the dependency needs to be used as a singleton.
  • The Red Pea
    The Red Pea about 7 years
    Just a note, if you use Angular2 (or maybe other applications?), there is some buzz around npm linking being root cause of specific kind of issue. Example here and here
  • user2167582
    user2167582 almost 7 years
    However keep in mind that npm link will create a second instance of external dependencies. So if you have a package A need B and C, B need C. linking B will cause application A to have two instances of C.
  • Camille Wintz
    Camille Wintz almost 7 years
    Unlike link, this uses .npmignore.
  • Jamie Birch
    Jamie Birch almost 7 years
    I wrote a TypeScript typings module for an existing JS project that I want to link locally. However, when I cd to the project directory and run the command npm link @types/promise-retry, it returns a 404 Not Found error, and the console log shows that it was trying to access the npm registry: https://registry.npmjs.org/@types%2fpromise-retry. Why is this bothering to connect to the registry at all, when it should be a completely local process?
  • Charles Holbrow
    Charles Holbrow almost 7 years
    It looks like npm 5 uses symbolic links by deafult when you npm install /path
  • Frank Tan
    Frank Tan over 6 years
    @bithavoc At least as of npm 5, installing a folder now creates a symlink, not a copy. See docs.npmjs.com/cli/install
  • Jules
    Jules over 6 years
    Note that this does not work for packages that have a prepublish step (e.g. if they need translating via babel before they can be used as a library). You'll need to use npm install (or run npm run prepublish manually every time you change the library) for such packages.
  • WitaloBenicio
    WitaloBenicio almost 6 years
    I tried to use this way, but my module can't find it's peerDependencies.
  • Renato Back
    Renato Back almost 6 years
    it's nice to rm -rf node_modules before and npm install after you run the answer's script.
  • Mehmet K
    Mehmet K over 5 years
    npm link creates 2 symlinks, 1 in the global scope and 1 in the importing package. npm install /path creates just 1 symlink in the importing package
  • Paul Medynski
    Paul Medynski over 5 years
    I found a workaround, which is to set NODE_PATH to point to the node_modules/ where foo is installed. So for the above case, it would be this: NODE_PATH=/dev/myproject/node_modules/ That allows mymodule to find foo.
  • Webwoman
    Webwoman about 5 years
    @smaudet probably their business is to store package hence local package is push on the secondary zone with very less consideration
  • webish
    webish almost 5 years
    i'm not able to do this without an uncaught exception in node v8.10.0
  • Michael
    Michael over 4 years
    @FrankTan Yes, but how to get the old behavior? I want the copy!
  • Frank Tan
    Frank Tan over 4 years
    @Michael, unfortunately, as far as I can tell from the docs, you can't get the old behavior anymore.
  • otoomey
    otoomey over 4 years
    This is a must for node addons, which require a rebuild in order to be used as a package. npm link will not do this.
  • Dip686
    Dip686 about 4 years
    I had a local package(say package1), package2 has a dependency mentioned with relative path of package1. npm i not installing the package when relative path starts with "file:../../package1" , working when it is ''../../package1", does adding file in the begining means anything else?
  • Ajmal Moochingal
    Ajmal Moochingal about 4 years
    There's a solution for. Put the dependency modules in project root folder. Define your dependencies in package.json with the usual 'file:' prefix. Do npm i This will create a symlink in project's node_modules as well as its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies. My npm version is v6.14.4 . After spending couple of hours on how to fix this, found this solution here : (atmos.washington.edu/~nbren12/reports/journal/…) . Thanks nbren12.
  • Ajmal Moochingal
    Ajmal Moochingal about 4 years
    @Michael There's a solution for that. Put the dependency modules in project root folder. Define your dependencies in package.json with the usual 'file:' prefix. Do npm i This will create a symlink in project's node_modules as well as its dependencies may be hoisted to the toplevel node_modules as they would for other types of dependencies. My npm version is v6.14.4 . After spending couple of hours on how to fix this, found this solution here : (atmos.washington.edu/~nbren12/reports/journal/…)
  • theawless
    theawless about 4 years
    I was having the same trouble. I found this answer: stackoverflow.com/questions/50807329/…, this solves my problem with peer dependencies and local libraries.
  • Alexey Sh.
    Alexey Sh. almost 4 years
    it throws an error for me. @typescript-eslint/[email protected] requires a peer of eslint@^5.0.0 but none is installed. You must install peer dependencies yourself.
  • jcubic
    jcubic almost 4 years
    unlike link this works for global install so you can test binary from you node module locally.
  • Misi
    Misi over 3 years
    I forgot to build my package before packing, so npm run build before.
  • Grant
    Grant about 3 years
    Can someone explain to me why I have to occasionally do this again? I am using Ionic Framework, when I sync my plugins, sometimes it will defer to the public version instead of using my local version and I have to re-link it -- is this tied to rebooting?
  • protoEvangelion
    protoEvangelion almost 3 years
    If you are in a yarn workspaces repo you can use yarn link & yarn link package-name
  • raythurnevoid
    raythurnevoid almost 3 years
    This is the best reply because it also install sub-dependencies!
  • Adam Jagosz
    Adam Jagosz almost 3 years
    npm --save? You mean npm i --save? (Which is now equivalent to npm i)
  • basickarl
    basickarl almost 3 years
    @AdamJagosz Fixed!
  • BEvo
    BEvo almost 3 years
    a caveat: using npm link ../my-module doesnt enforce type checking if my-module is a typescript module.
  • walnut_salami
    walnut_salami over 2 years
    If both the host package and the local package use eslint, you'll run into github.com/vuejs/vue-cli/issues/1494#issuecomment-498144990
  • user3032481
    user3032481 over 2 years
    Yeah, if you use file:<package_root_path> (not the path of the pack file) in the dependencies to install the package from your local file system. The local package will not be copied to your node_modules but instead it is linked into node_modules. With npm i, sub-dependencies can be installed automatically but the sub-dependencies cannot be shared with other packages. In this situation, the instanceof keyword may not work as expected if you want to use the keyword for the objects from the local project. So, I think npm pack + package.json is a reasonable solution.
  • user202729
    user202729 over 2 years
    @FrankTan Someone said that you can do the copy by packing it first then install from the pack → stackoverflow.com/a/69882320/5267751
  • Gwyneth Llewelyn
    Gwyneth Llewelyn about 2 years
    I did a few tests, and, indeed, it seems to work without the dot for designating the current directory, when you're already inside it.
  • Auguste Van Nieuwenhuyzen
    Auguste Van Nieuwenhuyzen about 2 years
    Is this meant to work on windows? Because it doesn't for me (npm 7.5.1)
  • forresthopkinsa
    forresthopkinsa almost 2 years
    How do you install local modules with this? This answer doesn't make sense to me.
  • Ben Brookes
    Ben Brookes almost 2 years
    When working with nx workspaces this is what solved the issue of local modules not being found.
  • forresthopkinsa
    forresthopkinsa almost 2 years
    PSA: Yarn struggles with this method due to overzealous caching. See yarnpkg/yarn#2165. I had to migrate my project (back) from Yarn to NPM for this.
  • mlhDev
    mlhDev almost 2 years
    To add the local dependency without editing the package.json file manually you can run npm install with the local path: npm install ../foo/bar --save updates the packages.json file the same way.