You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派4上编译serialport依赖时node-gyp出现Python语法错误

问题:树莓派4上含serialport的Electron应用编译失败

在树莓派4上运行Node.js Electron应用时遇到困境:最新版树莓派OS无法编译应用,但旧版OS无法适配最新硬件。已定位核心问题出在serialport包,即使是仅包含该包的极简Electron应用也编译失败。试过Node 12、14、16版本,以及serialport 9、10版本,均未解决。

执行npm i时出现如下错误:

> usb@2.5.0 install /home/myapp/myapp/node_modules/usb
> node-gyp-build

> lzma-native@8.0.6 install /home/myapp/myapp/node_modules/lzma-native
> node-gyp-build

gyp ERR! configure error 
gyp ERR! stack Error: Command failed: /usr/bin/python -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                       ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack 
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:308:12)
gyp ERR! stack     at ChildProcess.emit (events.js:314:20)
gyp ERR! stack     at maybeClose (internal/child_process.js:1022:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
gyp ERR! System Linux 5.15.56-v7l+
gyp ERR! command "/home/myapp/.config/nvm/versions/node/v12.22.12/bin/node" "/home/myapp/myapp/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/myapp/myapp/node_modules/lzma-native
gyp ERR! node -v v12.22.12
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN myapp-app@1.3.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: appdmg@0.4.5 (node_modules/appdmg):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for appdmg@0.4.5: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"arm"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! lzma-native@8.0.6 install: `node-gyp-build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the lzma-native@8.0.6 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/myapp/.npm/_logs/2022-08-26T13_57_08_118Z-debug.log

问题根源

错误日志明确显示:node-gyp调用的Python脚本使用了Python 2的字符串插值语法(print "%s.%s.%s"),但最新版树莓派OS默认安装的是Python 3,而你使用的node-gyp@3.8.0版本不兼容Python 3,导致语法错误。serialport依赖的lzma-native、usb等原生模块需要通过node-gyp编译,因此触发了这个问题。

解决步骤

  • 指定node-gyp使用Python 2

    1. 安装Python 2:
      sudo apt install python2
      
    2. 配置npm默认使用Python 2:
      npm config set python /usr/bin/python2
      
  • 升级node-gyp到兼容Python 3的版本

    1. 全局更新node-gyp:
      npm install -g node-gyp@latest
      
    2. 或在项目内更新:
      npm install node-gyp@latest --save-dev
      
  • 适配Electron的原生模块编译
    Electron自带独立的Node版本,需用electron-rebuild重新编译依赖:

    1. 安装electron-rebuild:
      npm install --save-dev electron-rebuild
      
    2. 执行重建:
      ./node_modules/.bin/electron-rebuild
      
  • 更新serialport到兼容版本
    尝试使用serialport@11及以上版本,这类版本对Python 3和新树莓派OS的支持更完善,同时搭配Node 16或18版本(树莓派OS对新版本Node的兼容性更好)

内容的提问来源于stack exchange,提问作者fingerskier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 04:06:25