node.js 使用 NPM 安装软件包时,您能告诉它使用其依赖项之一的不同版本吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11233133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:55:53  来源:igfitidea点击:

When installing a package with NPM, can you tell it to use a different version of one of its dependencies?

node.jsdependenciesnpm

提问by Lance Pollard

Say you want to install a library lib-awhich has dependencies dep-1and dep-2. If lib-ahas declared in its package.json to use a version of dep-2that 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-2that works with node 0.8.0 - branch name node0.8.0.

假设您要安装一个lib-a具有依赖项dep-1dep-2. 如果lib-a在它的 package.json 中声明使用一个dep-2过时的版本(说它在刚刚出现的节点 0.8.0 上不起作用),但是有一个分支dep-2可以用于节点 0.8.0 -分行名称node0.8.0

So the packages in the equation are:

所以等式中的包是:

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

Is there a way to tell NPM to install lib-a, but use dep-2#node0.8.0instead of dep-2?

有没有办法告诉 NPM 安装lib-a,但使用dep-2#node0.8.0而不是dep-2

With NPM you can install a specific branch of a project like this:

使用 NPM,您可以像这样安装项目的特定分支:

npm install git://github.com/user-c/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.0like this:

如果我要自定义 的 package.json lib-a,你可以告诉它dep-2#node0.8.0像这样使用:

{
  "name": "lib-a",
  "dependencies": {
    "dep-1": ">= 1.5.0",
    "dep-2": "git://github.com/user-c/dep-2#node0.8.0"
  }
}

By modifying the package.json you can then run

通过修改 package.json 然后你可以运行

npm install lib-a

and it will install the node 0.8.0 compatible dep-2branch. But, that requires I have access to modifying lib-a, which for my specific case I don't. Technically, I could fork lib-aand make the above change to package.json. But in my specific case, lib-ais a dependency of anotherlibrary, so I'd have to fork the project it's referenced in, and on and on...

它将安装 node 0.8.0 兼容dep-2分支。但是,这要求我有权访问 modify lib-a,而对于我的特定情况,我没有。从技术上讲,我可以 forklib-a并对 package.json 进行上述更改。但在我的特定情况下,lib-a另一个库的依赖项,所以我必须分叉它引用的项目,等等......

So the question is, is there a way to tell NPM to install lib-a, and tell it to use the node0.8.0branch of dep-2? Something like this:

所以问题是,有没有办法告诉 NPM 安装lib-a,并告诉它使用 的node0.8.0分支dep-2?像这样的东西:

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.

那将是真棒。如果不可能,那最好知道,这样我就可以为必须分叉/定制项目链做好准备。

采纳答案by micnic

NPM install syntax:

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 [email protected]

more info: https://docs.npmjs.com/cli/install

更多信息:https: //docs.npmjs.com/cli/install