在使用NPM安装软件包时,可以告诉它使用不同版本的依赖项吗? [英] When installing a package with NPM, can you tell it to use a different version of one of its dependencies?

查看:341
本文介绍了在使用NPM安装软件包时,可以告诉它使用不同版本的依赖项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要安装一个库 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 it's 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

如果我要自定义package.json lib-a ,您可以告诉它使用 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

,它将安装节点0.8.0兼容的 dep-2 分支。但是,这需要我有权修改 lib-a ,这对我的具体情况我不这样做。从技术上讲,我可以fork lib-a 并将上述更改变为package.json。但是在我的具体情况下, lib-a 是另一个库的依赖,所以我必须把它引用的项目分配到

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-2 node0.8.0 分支?如下所示:

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.

推荐答案

p> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆