【问题标题】:Node doesn't install child module dependencies when version is same as parent module当版本与父模块相同时,节点不安装子模块依赖项
【发布时间】:2014-06-09 19:30:09
【问题描述】:

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

我的父模块有两个依赖:

  • "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-uglify,那么node-snapshot 成功地在其node_modules 目录中安装grunt-contrib-uglify,并且一切都是tickety-boo。

我将如何解决这个问题?对我来说很明显,所有子模块都需要自己的安装,即使父模块具有相同的模块,因为相对而言,子模块 (node-snapshot) 无法找到其依赖项之一。

【问题讨论】:

  • 即使省略路径也不能解决问题:grunt build:production.

标签: node.js heroku npm


【解决方案1】:

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

GitHub 上的以下主题提供了非常丰富的信息:https://github.com/npm/npm/issues/5001

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

在我使用postinstall 的特殊情况下,解决方法是添加带有--ignore-scripts 标志的preinstall,以防止递归调用scripts 挂钩。这使npm install 能够独立运行,从而安装所有依赖项,而与父模块无关。

因此,我的 package.json 现在看起来几乎完全相同,但带有 preinstall 钩子:

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

【讨论】:

    【解决方案2】:

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

    "postinstall": "grunt build:production"
    

    【讨论】:

    • 还是一样。相反,我现在尝试删除 postinstall 并将其放入 Travis 配置中,但这不起作用。
    猜你喜欢
    • 2017-07-25
    • 2013-08-26
    • 1970-01-01
    • 2014-02-17
    • 2017-04-20
    • 1970-01-01
    • 2013-08-01
    • 2023-01-14
    • 2023-04-11
    相关资源
    最近更新 更多