electron-rebuild 编译遇到的奇葩问题

本文记录了在Windows系统下,使用Electron-rebuild进行模块重建时遇到的node-gyp编译错误。问题源于Python版本不兼容。作者通过手动修改源码并指定Python版本,成功解决了报错,使得Electron应用能够正常编译。此外,还分享了检查npm执行时所用Python配置的方法。
摘要由CSDN通过智能技术生成
.\node_modules\.bin\electron-rebuild.cmd

环境:

系统:win10
"dependencies": {
    "core-js": "^3.6.5",
    "electron-rebuild": "^3.2.3",
    "escpos-serialport": "^3.0.0-alpha.4",
    "vue": "^2.6.11"
},
"devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "electron": "^13.0.0",
    "electron-devtools-installer": "^3.1.0",
    "electron-prebuilt": "^1.4.13",
    "electron-rebuild": "^3.2.3",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "serialport": "^9.2.1",
    "vue-cli-plugin-electron-builder": "~2.1.1",
    "vue-template-compiler": "^2.6.11"
 },

小知识:你安装electron-rebuild之后要把它运动到dependencies中

执行之后报错如下:

 File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\gyp_main.py", line 45, in <module>
    sys.exit(gyp.script_main())
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\__init__.py", line 662, in script_main
    return main(sys.argv[1:])
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\__init__.py", line 654, in main
    return gyp_main(args)
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\__init__.py", line 629, in gyp_main
    options.circular_check,
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\__init__.py", line 151, in Load
    params["root_targets"],
⠼ Building module: bindings, Completed: 0  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 3010, in Load  
    build_file, data, aux_data, variables, includes, depth, check, True
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 428, in LoadTargetBuildFile
    build_file_data, PHASE_EARLY, variables, build_file_path
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 1411, in ProcessVariablesAndConditionsInDict
    ProcessVariablesAndConditionsInList(value, phase, variables, build_file)
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 1424, in ProcessVariablesAndConditionsInList
    ProcessVariablesAndConditionsInDict(item, phase, variables, build_file)
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 1411, in ProcessVariablesAndConditionsInDict
    ProcessVariablesAndConditionsInList(value, phase, variables, build_file)
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 1428, in ProcessVariablesAndConditionsInList
    expanded = ExpandVariables(item, phase, variables, build_file)
  File "F:\my\code\electron\my-electron\node_modules\_node-gyp@8.2.0@node-gyp\gyp\pylib\gyp\input.py", line 979, in ExpandVariables
    p_stdout = p_stdout.decode("utf-8")
AttributeError: 'str' object has no attribute 'decode' while trying to load binding.gyp
✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'F:\my\code\electron\my-electron\node_modules\_@serialport_bindings@9.2.1@@serialport\bindings'.
For more information, rerun with the DEBUG environment variable set to "electron-rebuild".

Error: `gyp` failed with exit code: 1

Error: node-gyp failed to rebuild 'F:\my\code\electron\my-electron\node_modules\_@serialport_bindings@9.2.1@@serialport\bindings'.

就很 "shabi",我系统的python版本就是2.7.3的(也切换3.7尝试也不行),可是还是报这个错误。

最后解决方案就是修改源码(同时也把python版本指向2.7)

1.编辑器打开这个源码文件

 2.修改源码

 然后再次运行

.\node_modules\.bin\electron-rebuild.cmd

 就搞定了,真 "shabi"。

补充,下面查看npm执行的时候使用的配置

npm config get userconfig

 

把这里的python设置成python,就会根据你配置的环境变量来使用python版本

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

An_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值