使用 NPM 安装包时,你能告诉它使用其依赖项之一的不同版本吗? [英] When installing a package with NPM, can you tell it to use a different version of one of its dependencies?
问题描述
假设您要安装一个库 lib-a
,它具有依赖项 dep-1
和 dep-2
.如果 lib-a
在其 package.json 中声明使用过期的 dep-2
版本(假设它在节点 0.8.0 上不起作用刚刚出来),但是 dep-2
的一个分支适用于节点 0.8.0 - 分支名称 node0.8.0
.
Say you want to install a library lib-a
which has dependencies dep-1
and dep-2
. If lib-a
has declared in its package.json to use a version of dep-2
that is out of date (say it doesn't work on node 0.8.0 which just came out), but there is a branch of dep-2
that works with node 0.8.0 - branch name node0.8.0
.
所以等式中的包是:
git://github.com/user-a/lib-a
git://github.com/user-b/dep-1
git://github.com/user-c/dep-2
git://github.com/user-c/dep-2#node0.8.0
有没有办法告诉 NPM 安装 lib-a
,但是使用 dep-2#node0.8.0
而不是 dep-2
?
Is there a way to tell NPM to install lib-a
, but use dep-2#node0.8.0
instead of dep-2
?
使用 NPM,您可以像这样安装项目的特定分支:
With NPM you can install a specific branch of a project like this:
npm install git://github.com/user-c/dep-2#node0.8.0
如果我要自定义 lib-a
的 package.json,你可以告诉它像这样使用 dep-2#node0.8.0
:
And if I were to customize the package.json of lib-a
, you could tell it to use dep-2#node0.8.0
like this:
{
"name": "lib-a",
"dependencies": {
"dep-1": ">= 1.5.0",
"dep-2": "git://github.com/user-c/dep-2#node0.8.0"
}
}
通过修改 package.json 就可以运行
By modifying the package.json you can then run
npm install lib-a
它会安装 node 0.8.0 兼容的 dep-2
分支.但是,这需要我有权修改 lib-a
,而对于我的具体情况,我没有.从技术上讲,我可以 fork lib-a
并对 package.json 进行上述更改.但在我的具体情况下, lib-a
是 another 库的依赖项,所以我必须分叉它所引用的项目,然后继续......
and it will install the node 0.8.0 compatible dep-2
branch. But, that requires I have access to modifying lib-a
, which for my specific case I don't. Technically, I could fork lib-a
and make the above change to package.json. But in my specific case, lib-a
is a dependency of another library, so I'd have to fork the project it's referenced in, and on and on...
那么问题来了,有没有办法告诉 NPM 安装 lib-a
,并告诉它使用 dep 的
?像这样的:node0.8.0
分支-2
So the question is, is there a way to tell NPM to install lib-a
, and tell it to use the node0.8.0
branch of dep-2
? Something like this:
npm install lib-a --overrides dep-2:git://github.com/user-c/dep-2#node0.8.0
那太棒了.如果这是不可能的,那会很高兴知道这样我就可以准备好必须分叉/自定义项目链.
That would be awesome. If it's not possible, that would be good to know so I can prepare myself to have to fork/customize the chain of projects.
推荐答案
NPM安装语法:
npm install (with no args in a package dir)
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install [@<scope>/]<name> [--save|--save-dev|--save-optional] [--save-exact]
npm install [@<scope>/]<name>@<tag>
npm install [@<scope>/]<name>@<version>
npm install [@<scope>/]<name>@<version range>
npm i (with any of the previous argument usage)
因此您可以选择其中一种方法来安装您的模块.
so you can choose one of these methods to install your modules.
安装特定版本的最简单方法是这种情况:
The case of the simplest way to install a specific version is this one:
npm install module@0.0.2
更多信息:https://docs.npmjs.com/cli/install
这篇关于使用 NPM 安装包时,你能告诉它使用其依赖项之一的不同版本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!