Installing a local module using npm?
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 :(
fancy
Updated on February 10, 2022Comments
-
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 over 10 yearsIt might not be original intent of the question, but it's probably what most people who find this through google want.
-
Thomas Potaire over 9 yearsThis answer seems incomplete, you need to run
npm link
against the folder once (to create a global symlink) and then runnpm 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 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 over 8 yearsThis 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 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 over 7 years@TylerCollier npm unlink appears to be the mirror-image operation stackoverflow.com/a/24940024/54426
-
Daniel Waltrip over 7 yearsThe second method (using the
file:
approach) allowed for my app and the local module to share a dependency. My test ofnpm link
resulted in a duplicate dependency, which breaks things if the dependency needs to be used as a singleton. -
The Red Pea about 7 years
-
user2167582 almost 7 yearsHowever 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 almost 7 yearsUnlike link, this uses .npmignore.
-
Jamie Birch almost 7 yearsI 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 commandnpm 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 almost 7 yearsIt looks like npm 5 uses symbolic links by deafult when you
npm install /path
-
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 over 6 yearsNote 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 usenpm install
(or runnpm run prepublish
manually every time you change the library) for such packages. -
WitaloBenicio almost 6 yearsI tried to use this way, but my module can't find it's peerDependencies.
-
Renato Back almost 6 yearsit's nice to
rm -rf node_modules
before andnpm install
after you run the answer's script. -
Mehmet K over 5 yearsnpm 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 over 5 yearsI found a workaround, which is to set
NODE_PATH
to point to thenode_modules/
wherefoo
is installed. So for the above case, it would be this:NODE_PATH=/dev/myproject/node_modules/
That allowsmymodule
to findfoo
. -
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 almost 5 yearsi'm not able to do this without an uncaught exception in node v8.10.0
-
Michael over 4 years@FrankTan Yes, but how to get the old behavior? I want the copy!
-
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 over 4 yearsThis 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 about 4 yearsI 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 about 4 yearsThere'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 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 about 4 yearsI 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. almost 4 yearsit 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 almost 4 yearsunlike link this works for global install so you can test binary from you node module locally.
-
Misi over 3 yearsI forgot to build my package before packing, so npm run build before.
-
Grant about 3 yearsCan 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 almost 3 yearsIf you are in a yarn workspaces repo you can use
yarn link
&yarn link package-name
-
raythurnevoid almost 3 yearsThis is the best reply because it also install sub-dependencies!
-
Adam Jagosz almost 3 years
npm --save
? You meannpm i --save
? (Which is now equivalent tonpm i
) -
basickarl almost 3 years@AdamJagosz Fixed!
-
BEvo almost 3 yearsa caveat: using npm link ../my-module doesnt enforce type checking if my-module is a typescript module.
-
walnut_salami over 2 yearsIf both the host package and the local package use eslint, you'll run into github.com/vuejs/vue-cli/issues/1494#issuecomment-498144990
-
user3032481 over 2 yearsYeah, if you use
file:<package_root_path>
(not the path of the pack file) in thedependencies
to install the package from your local file system. The local package will not be copied to yournode_modules
but instead it is linked intonode_modules
. Withnpm i
, sub-dependencies can be installed automatically but the sub-dependencies cannot be shared with other packages. In this situation, theinstanceof
keyword may not work as expected if you want to use the keyword for the objects from the local project. So, I thinknpm pack
+package.json
is a reasonable solution. -
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 about 2 yearsI 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 about 2 yearsIs this meant to work on windows? Because it doesn't for me (npm 7.5.1)
-
forresthopkinsa almost 2 yearsHow do you install local modules with this? This answer doesn't make sense to me.
-
Ben Brookes almost 2 yearsWhen working with nx workspaces this is what solved the issue of local modules not being found.
-
forresthopkinsa almost 2 yearsPSA: 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 almost 2 yearsTo 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.