当版本与父模块相同时,节点不会安装子模块依赖项

通常情况下,我可以在Google上发现任何我遇到的问题,但在这种情况下,我绝对不知道要search什么。

我的父模块有两个依赖关系:

  • "grunt-contrib-uglify": "~0.2.7"
  • "node-snapshot": "~0.3.22"

但是,当node-snapshot也依赖于~0.2.7 ,它不会安装在node-snapshotnode_modules目录中。 我的Snapshot应用程序通过Travis自动部署到Heroku,因此具有:

 "scripts": { "test": "grunt test", "start": "node example/server/default.js", "postinstall": "node_modules/grunt-cli/bin/grunt build:production" } 

在将它安装到父模块( npm install node-snapshot )之后,也会调用它,但是由于父项和子项均依赖于grunt-contrib-uglify ~0.2.7

 >> Local Npm module "grunt-contrib-uglify" not found. Is it installed? Warning: Task "uglify" not found. Use --force to continue. 

如果父模块依赖于不同版本的grunt-contrib-uglifynode-snapshot将在其node_modules目录中成功安装grunt-contrib-uglify node_modules并且所有内容都是有争议的。

我将如何去解决这个问题? 很明显,所有的孩子都需要自己的安装,即使父母有相同的模块,因为相对的,子模块( node-snapshot )无法find它的一个依赖关系。

经过昨天的一些研究,这显然是npm的一个已知问题。 有很多关于子模块依赖关系没有被安装的线程,因为父模块满足了特定的依赖关系。

下面的线程特别在GitHub上是非常丰富的: https : //github.com/npm/npm/issues/5001

目前没有解决scheme ,但有解决方法。

在我postinstall的特殊情况下,解决方法是添加带有--ignore-scripts标志的preinstall ,防止scripts钩子被recursion调用。 这使得npm install可以独立运行,从而安装所有的依赖项而不pipe父模块。

因此,我的package.json现在看起来几乎完全一样,但是使用preinstall钩子:

 "scripts": { "test": "grunt test", "start": "node example/server/default.js", "preinstall": "npm install --ignore-scripts", "postinstall": "bower install; grunt build:production" } 

npm在可以帮助时不会安装重复的模块。 它也不需要运行脚本的path。 当你做什么时会发生什么:

 "postinstall": "grunt build:production"