从Git安装node-mbus npm包失败的原因及解决方法
node-mbus从Git仓库安装失败的解决方法
问题现象
- 在package.json中指定npm版本号时可正常安装:
... "dependencies": { "node-mbus": "^2.0.0" } ... - 改为直接使用Git仓库地址后,安装报错:
错误信息:... "dependencies": { "node-mbus": "https://github.com/Apollon77/node-mbus" } ...npm ERR! make: *** No rule to make target 'Release/obj.target/libmbus/libmbus/mbus/mbus-protocol-aux.o', needed by 'Release/obj.target/mbus.a'. Stop. - 其他包(如xml2js)无论指定版本还是Git地址都能正常安装,排除依赖地址本身的问题。
排查过程
- 克隆node-mbus仓库到本地后,执行
npm i file:<本地克隆路径>可正常完成安装; - 使用
npm i --ignore-scripts https://github.com/Apollon77/node-mbus跳过脚本安装后,手动执行node-gyp configure; node-gyp build也能成功编译。
问题原因与解决方案
原脚本配置问题
node-mbus原package.json中的scripts配置如下:
... "scripts": { "prepare": "rm -rf libmbus && git clone -b build-windows --single-branch https://github.com/Apollon77/libmbus.git libmbus && rm libmbus/.gitignore && cp config.h libmbus/config.h", "install": "node-gyp rebuild", "test": "nyc --reporter=lcov node_modules/mocha/bin/mocha --exit", "release": "release-script" }, ...
当从Git仓库安装包时,npm的脚本执行顺序导致prepare脚本在编译步骤之后才执行,此时libmbus依赖还未被拉取,进而引发编译错误。
修改方案
将prepare脚本改为preinstall,确保在install脚本(执行node-gyp rebuild)之前完成libmbus的拉取与配置:
... "scripts": { "preinstall": "rm -rf libmbus && git clone -b build-windows --single-branch https://github.com/Apollon77/libmbus.git libmbus && rm libmbus/.gitignore && cp config.h libmbus/config.h", "install": "node-gyp rebuild", "test": "nyc --reporter=lcov node_modules/mocha/bin/mocha --exit", "release": "release-script" }, ...
修改后重新执行安装即可正常完成。
内容的提问来源于stack exchange,提问作者Shorosky
相关产品推荐
相关产品推荐

