使用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您只需向参数> ,参数应指向本地文件夹而不是包名称:
npm install
you just provide one
<folder>
argument tonpm install
, argument should point toward the local folder instead of the package name:来自 npm-link 文档:
在本地模块目录中:
在要使用的项目目录中模块:
或者一次性使用相对路径:
这相当于在后台使用上面的两个命令。
From the npm-link documentation:
In the local module directory:
In the directory of the project to use the module:
Or in one go using relative paths:
This is equivalent to using two commands above under the hood.
由于提问和回答是同一个人,我将添加一个 npm 链接 作为替代方案。
来自文档:
[编辑] 从 NPM 2.0 开始,您可以在 package.json 中声明本地依赖项
Since asked and answered by the same person, I'll add a npm link as an alternative.
from docs:
[Edit] As of NPM 2.0, you can declare local dependencies in package.json
npm pack
+package.json
这对我有用:
第1步:在
模块项目
中,执行npm pack
>:这将构建一个
-.tar.gz
文件。第 2 步:将文件移动到
consumer project
理想情况下,您可以将所有此类文件放入
consumer-project
根目录中的tmp
文件夹中:STEP 3:在
package.json
中引用它:第 4 步:
安装
软件包:npm install
或npm i
或yarn
现在,你的该包将在您的消费者项目的 node_modules 文件夹中提供。
祝你好运...
npm pack
+package.json
This is what worked for me:
STEP 1: In
module project
, executenpm 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 yourconsumer-project
root:STEP 3: Refer it in your
package.json
:STEP 4:
Install
the packages:npm install
ornpm i
oryarn
Now, your package would be available in your
consumer-project's node_modules
folder.Good Luck...
如果本地模块具有您只想在项目范围内安装的对等依赖项,那么这两种方法(
npm link
或package.json
文件依赖项)都不起作用。例如:
/local/mymodule/package.json
/dev/myproject/package.json
在这种情况下,npm 设置
myproject
的node_modules/
像这样:当节点加载
mymodule
并执行require('foo')
时,节点会解析mymodule
> 符号链接,然后只查找/local/mymodule/node_modules/
(及其祖先)用于foo
,但它找不到。相反,我们希望节点在/local/myproject/node_modules/
中查找,因为这是我们运行项目的位置,也是安装foo
的位置。因此,我们要么需要一种方法来告诉节点在查找
foo
时不解析此符号链接,或者我们需要一种方法来告诉 npm 安装副本<当在package.json
中使用文件依赖语法时,mymodule
的 /em>。不幸的是,我还没有找到一种方法:(Neither of these approaches (
npm link
orpackage.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
/dev/myproject/package.json
In this scenario, npm sets up
myproject
'snode_modules/
like this:When node loads
mymodule
and it doesrequire('foo')
, node resolves themymodule
symlink, and then only looks in/local/mymodule/node_modules/
(and its ancestors) forfoo
, 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 wherefoo
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 ofmymodule
when the file dependency syntax is used inpackage.json
. I haven't found a way to do either, unfortunately :(缺少主要财产?
正如之前的人回答的那样,
npm i --save ../location-of-your-packages-root-directory
。然而,
../location-of-your-packages-root-directory
必须具备两件事才能正常工作。package.json
指向package.json
中的main
属性,必须设置并工作"main ": "src/index.js",
如果../location-of-your-packages-root-directory
的入口文件是../location-of-your-packages-root-directory/src/index.js
Missing the main property?
As previous people have answered
npm i --save ../location-of-your-packages-root-directory
.The
../location-of-your-packages-root-directory
however must have two things in order for it to work.package.json
in that directory pointed towardsmain
property in thepackage.json
must be set and working i.g."main": "src/index.js",
if the entry file for../location-of-your-packages-root-directory
is../location-of-your-packages-root-directory/src/index.js
所以我对到目前为止提到的所有解决方案都遇到了很多问题...
我有一个我想始终引用的本地包(而不是 npm 链接),因为它不会在该项目之外使用(目前)并且目前还不会上传到 npm 存储库以供广泛使用。
我还需要它在 Windows 和 Unix 上工作,所以符号链接并不理想。
指向(npm 包)的 tar.gz 结果适用于依赖的 npm 包文件夹,但是如果您想更新包,这会导致 npm 缓存出现问题。当你更新它时,它并不总是从引用的 npm 包中提取新的,即使你删除了 node_modules 并为你的主项目重新安装了 npm-install 。
所以..这对我来说很有效!
主项目的 Package.json 文件片段:
这实现了 3 件事:
我希望这是清楚的,并能帮助别人。
tar.gz 方法也有点工作
。npm install(文件路径)也有点工作。
这一切都是基于 openapi 规范生成的客户端,我们希望将其保留在单独的位置(而不是对单个文件使用 copy-pasta)
======
更新:
======
使用上述解决方案进行常规开发流程还会出现其他错误,因为 npm 的本地文件版本控制方案非常糟糕。如果您的依赖包经常更改,整个方案就会被破坏,因为 npm 会缓存项目的最后一个版本,然后当 SHA 哈希值与 package-lock.json 文件中保存的内容不再匹配时就会崩溃,以及其他问题。
因此,我建议使用 *.tgz 方法,并为每次更改进行版本更新。这是通过做三件事来实现的。
首先:
对于您的依赖包,请使用 npm 库“ng-packagr”。这会自动添加到由 OpenAPI 3.0 的 Angular-TypeScript 代码生成器创建的自动生成的客户端包中。
因此,我引用的项目在 package.json 中有一个“脚本”部分,如下所示:
引用其他项目的项目添加了一个预安装步骤,以确保依赖项目是最新的并重新构建在构建自身之前:
第二
从主项目引用构建的 tgz npm 包!
第三
每次更新依赖包时都更新依赖包的版本。您还必须更新主项目中的版本。
如果您不这样做,NPM 将阻塞并使用缓存版本,并在 SHA 哈希不匹配时爆炸。 NPM 根据文件名更改来版本基于文件的包。它不会检查包本身是否有 package.json 中的更新版本,NPM 团队表示他们不会修复此问题,但人们不断提出这个问题:https://github.com/microsoft/WSL/issues/348
现在,只需更新:
在依赖包的 package.json 文件中,然后更新您在主项目中对它的引用以供参考新的文件名,例如:
你已经习惯了。只需更新两个 package.json 文件 - 版本,然后引用新文件名。
希望能帮助某人...
So I had a lot of problems with all of the solutions mentioned so far...
I have a local package that I want to always reference (rather than npm link) because it won't be used outside of this project (for now) and also won't be uploaded to an npm repository for wide use as of yet.
I also need it to work on Windows AND Unix, so sym-links aren't ideal.
Pointing to the tar.gz result of (npm package) works for the dependent npm package folder, however this causes issues with the npm cache if you want to update the package. It doesn't always pull in the new one from the referenced npm package when you update it, even if you blow away node_modules and re-do your npm-install for your main project.
so.. This is what worked well for me!
Main Project's Package.json File Snippet:
This achieves 3 things:
I hope this is clear, and helps someone out.
The tar.gz approach also sort of works..
npm install (file path) also sort of works.
This was all based off of a generated client from an openapi spec that we wanted to keep in a separate location (rather than using copy-pasta for individual files)
======
UPDATE:
======
There are additional errors with a regular development flow with the above solution, as npm's versioning scheme with local files is absolutely terrible. If your dependent package changes frequently, this whole scheme breaks because npm will cache your last version of the project and then blow up when the SHA hash doesn't match anymore with what was saved in your package-lock.json file, among other issues.
As a result, I recommend using the *.tgz approach with a version update for each change. This works by doing three things.
First:
For your dependent package, use the npm library "ng-packagr". This is automatically added to auto-generated client packages created by the angular-typescript code generator for OpenAPI 3.0.
As a result the project that I'm referencing has a "scripts" section within package.json that looks like this:
And the project referencing this other project adds a pre-install step to make sure the dependent project is up to date and rebuilt before building itself:
Second
Reference the built tgz npm package from your main project!
Third
Update the dependent package's version EVERY TIME you update the dependent package. You'll also have to update the version in the main project.
If you do not do this, NPM will choke and use a cached version and explode when the SHA hash doesn't match. NPM versions file-based packages based on the filename changing. It won't check the package itself for an updated version in package.json, and the NPM team stated that they will not fix this, but people keep raising the issue: https://github.com/microsoft/WSL/issues/348
for now, just update the:
In the dependent package's package.json file, then update your reference to it in the main project to reference the new filename, ex:
You get used to it. Just update the two package.json files - version then the ref to the new filename.
Hope that helps someone...
在为 CKEditor5 安装自定义构建包时,我遇到了与上述不同的解决方案。
因此,我将包上传到应用程序根目录,然后:
在我的 package.json 中,包被列为文件:
我认为这个答案可能与如何添加本地包的主题相关。
I came across different solution than above while installing custom build package for CKEditor5.
So I uploaded package to app root directory, than:
In my package.json package is listed as a file:
I think this answer could be relevant to the topic on how to add local package.
正如 @fancy 接受的答案中所解释的,您可以使用此命令:
添加本地软件包。
在项目的
package.json
中,它将创建一个条目,如下所示:如果您包含的包位于项目根目录中,那么它将安装包含的本地包的所有依赖项。否则,即如果它位于项目根目录之外,它只会创建一个符号链接(如 @frank-tan 指出的那样),在这种情况下,如果由于某种原因您删除了
node_modules
目录您的项目或者需要重新安装,您必须运行:命令行选项
install-links
确保自动安装本地软件包的所有依赖项。例如,如果您正在使用 Jenkins 并且需要部署具有许多自定义开发的嵌套依赖项的大型项目,这将派上用场。请参阅官方 npm-install 文档了解更多详细信息:
https://docs.npmjs.com/cli/v9/commands/npm-安装
As explained in the accepted answer by @fancy, you can use this command:
to add your local packages.
In the
package.json
of your project it will create an entry like:If the package you're including is within the project root, then it will do an installation of all the dependencies of your included local package. Otherwise, i.e. if it's outside your project root, it will simply create a symbolic link (as @frank-tan pointed out), in which case, if for some reason you've deleted the
node_modules
directory in your project or you need to do a fresh reinstall you must run:The command line option
install-links
ensures that all dependencies of the local packages get installed automatically. This will come in handy if, for example, you're using Jenkins and need to deploy a large project with many custom-developed nested dependencies.See official npm-install documentation for more detail:
https://docs.npmjs.com/cli/v9/commands/npm-install
对于安装本地模块/包,尚未在 npm 上安装,或者您正在开发 npm 包并希望在发布之前在本地测试它。您可以尝试此操作 -
转到模块/包文件夹,然后 -
您的包已准备好使用,现在转到您想要安装的项目 -
包将安装到您的项目中。如果你想删除它 -
For installing local module / package, that not yet on npm or you are developing an npm package and want to test it locally before publishing it. You can try this -
Go to the module/package folder then -
Your packakge is ready to use, now go the project you want to install it -
Package will be installed to you project. If you want to remove it -
保持简单!
您可以使用 lnk-cli 创建目录链接。
这是我使用 TypeScript 的设置:
.ts
文件)第 1 步:安装 lnk-cli
步骤 2:公用文件夹
第 3 步:Package.json
步骤 4:.gitignore
完毕!
Keep It Simple!
You can use lnk-cli to create a directory link.
Here’s my setup using TypeScript:
.ts
files I want to share)Step 1: Install lnk-cli
Step 2: Common Folder
Step 3: Package.json
Step 4: .gitignore
Done!
对于更新版本的 npm(我在 macOS Big Sur 下使用 8.1.3),命令序列甚至更简单...
这将要求您提供一些与项目相关的数据并正确初始化您的项目。 json 文件。
完成后,您可以安装其他模块:
这就是您所需要的!
注意:我相信如果您在项目目录中,则不需要尾随点,但我也认为添加它并没有什么坏处:-)
(我想知道为什么官方文档还是不解释这个...)
For more recent versions of npm (I'm using 8.1.3 under macOS Big Sur), the sequence of commands is even easier...
This will ask you for some data related to your project and properly initialises your
project.json
file.Once that is done, you can install additional modules with:
That's all you need!
Note: I believe that the trailing dot is not necessary if you're inside the project directory, but I also think that it doesn't hurt to add it :-)
(I wonder why the official docs still don't explain this...)