skinny
V2EX  ›  Node.js

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

  •  
  •   skinny · Aug 2, 2021 · 4527 views
    This topic created in 1745 days ago, the information mentioned may be changed or developed.

    我在使用 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 ?

    Supplement 1  ·  Aug 2, 2021
    好吧,解决了,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
    Supplement 2  ·  Aug 3, 2021
    强制使用指定版本 node-gyp 的三个办法 : https://github.com/npm/cli/issues/2839#issuecomment-826416156
    1 replies    2021-08-03 17:43:54 +08:00
    bruceczk
        1
    bruceczk  
       Aug 3, 2021   ❤️ 1
    用 yarn 的话可以在 package.json 里指定 resolutions 强制所有包用某个版本的依赖。
    {
    "resolutions": {
    "**/eslint": "6.0.0"
    }
    }

    用 npm 的话可以装一个包 npm-force-resolutions,效果是一样的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1518 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 16:45 · PVG 00:45 · LAX 09:45 · JFK 12:45
    ♥ Do have faith in what you're doing.