V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
skinny
V2EX  ›  Node.js

请问怎么强制一个项目的 node_modules 下的任何级别的子模块都使用指定的 node-gyp 版本?

  •  
  •   skinny · 2021-08-02 20:49:36 +08:00 · 3667 次点击
    这是一个创建于 990 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在使用 npm 安装 electron-rebuild 时,总会在安装它的依赖 lzma-native 时报错:

    “Undefined variable module_path in binding.gyp while trying to load binding.gyp”

    但如果我自己安装最新的 node-gyp,再安装 electron-rebuild 所依赖的相同版本 lzma-native 就一切正常,

    我试图使用一些办法强制指定 node-gyp 版本,比如把 node-gyp 全局安装或安装在项目下,然后再用网传的魔法命令 npm config set node-gyp "<node-gyp.js path>"指定版本,都依然无法避免 electron-rebuild 一意孤行的使用它的低版本 node-gyp,请问有什么办法指定 node-gyp 的版本吗?

    难道必须换掉 npm ?

    第 1 条附言  ·  2021-08-02 21:31:24 +08:00
    好吧,解决了,npm 7.x 和 node-gyp 7 都有 bug……npm 7.x 的 bug 就是无法通过环境变量、配置变量、命令行参数这些办法指定 node-gyp,退回 npm 6.x,安装最新 node-gyp 8.1,然后再指定 node-gyp 就成功安装了……

    https://github.com/npm/cli/issues/2839
    第 2 条附言  ·  2021-08-03 18:56:48 +08:00
    强制使用指定版本 node-gyp 的三个办法 : https://github.com/npm/cli/issues/2839#issuecomment-826416156
    1 条回复    2021-08-03 17:43:54 +08:00
    bruceczk
        1
    bruceczk  
       2021-08-03 17:43:54 +08:00   ❤️ 1
    用 yarn 的话可以在 package.json 里指定 resolutions 强制所有包用某个版本的依赖。
    {
    "resolutions": {
    "**/eslint": "6.0.0"
    }
    }

    用 npm 的话可以装一个包 npm-force-resolutions,效果是一样的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1148 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:08 · PVG 07:08 · LAX 16:08 · JFK 19:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.