我想使用“咕哝-贡献-茉莉花”NPM包。它有各种依赖关系。依赖关系图的一部分是这样的:
─┬ grunt-contrib-jasmine@0.4.1
│ ├─┬ grunt-lib-phantomjs@0.2.0
│ │ ├─┬ phantomjs@1.8.2-2
不幸的是,这个版本的phantomjs有一个错误,阻止它在Mac OS x上正确安装。这在最新版本中得到了修复。
我怎么能让grunt-lib-phantomjs使用一个新的版本的phantomjs?
一些附加上下文:
Grunt-contrib-jasmine明确要求版本为“~0.2.0”的grunt-lib-phantomjs,它明确要求版本为“~1.8.1”的phantomjs。
首先将phantomjs添加到包的依赖项中没有效果;两个版本都安装了,而unt-contrib-jasmine仍然使用旧版本(参见:当使用NPM安装一个包时,你可以告诉它使用一个依赖项的不同版本吗?)
唯一适合我的解决方案(节点12。npm 6.x)使用了@Rogerio Chaves开发的npm-force-resolution。
首先,通过以下方法安装:
npm install npm-force-resolutions --save-dev
如果某些损坏的传递依赖脚本阻止您安装任何东西,您可以添加——ignore-scripts。
然后是包装。Json定义什么依赖应该被覆盖(你必须设置准确的版本号):
"resolutions": {
"your-dependency-name": "1.23.4"
}
在“脚本”部分添加新的preinstall条目:
"preinstall": "npm-force-resolutions",
现在,npm install将应用更改,并强制your- dependencies -name为所有依赖项的1.23.4版本。
唯一适合我的解决方案(节点12。npm 6.x)使用了@Rogerio Chaves开发的npm-force-resolution。
首先,通过以下方法安装:
npm install npm-force-resolutions --save-dev
如果某些损坏的传递依赖脚本阻止您安装任何东西,您可以添加——ignore-scripts。
然后是包装。Json定义什么依赖应该被覆盖(你必须设置准确的版本号):
"resolutions": {
"your-dependency-name": "1.23.4"
}
在“脚本”部分添加新的preinstall条目:
"preinstall": "npm-force-resolutions",
现在,npm install将应用更改,并强制your- dependencies -name为所有依赖项的1.23.4版本。
@user11153的答案在本地为我工作,但当试图做一个干净的安装(也就是删除node_modules),我会得到:
npm-force-resolutions: command not found
我必须更新预安装脚本为:
"preinstall": "npm i npm-force-resolutions && npm-force-resolutions"
这确保在试图运行npm-force-resolutions包之前安装了它。
也就是说,如果你可以用纱线代替,我会这样做,然后使用@Gus的答案。
从NPM v8.3开始,处理这个问题的正确方法是通过包的overrides部分。json文件。
If you need to make specific changes to dependencies of your
dependencies, for example replacing the version of a dependency with a
known security issue, replacing an existing dependency with a fork, or
making sure that the same version of a package is used everywhere,
then you may add an override.
Overrides provide a way to replace a package in your dependency tree
with another version, or another package entirely. These changes can
be scoped as specific or as vague as desired.
To make sure the package foo is always installed as version 1.0.0 no
matter what version your dependencies rely on:
{
"overrides": {
"foo": "1.0.0"
}
}
还有其他各种更细致的配置,允许您仅在某个包是特定包层次结构的依赖项时覆盖该包。欲了解更多详情,请访问https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides
你可以使用npm shrinkwrap功能,以覆盖任何依赖项或子依赖项。
我刚在我们的一个小项目里做过这个。从2.7.3开始,我们需要一个更新的connect版本。给我们带来了麻烦。所以我创建了一个名为npm-shrinkwrap.json的文件:
{
"dependencies": {
"grunt-contrib-connect": {
"version": "0.3.0",
"from": "grunt-contrib-connect@0.3.0",
"dependencies": {
"connect": {
"version": "2.8.1",
"from": "connect@~2.7.3"
}
}
}
}
}
NPM应该在为项目安装时自动拾取它。
(参见:https://nodejs.org/en/blog/npm/managing-node-js-dependencies-with-shrinkwrap/)