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

Electron+electron-builder使用ffi-napi时出现原生回调错误求助

解决Electron中使用ffi-napi时出现的"Error in native callback"错误

问题根源

这个错误是因为ffi-napi依赖的原生模块(如ref-napi)是基于系统Node.js版本编译的,而Electron自带独立的Node.js环境,版本与系统Node不一致,导致二进制模块不兼容,无法正常加载。

解决步骤

  1. 清理现有依赖与缓存

    • 删除node_modules文件夹:
      # Windows
      rd /s /q node_modules
      # macOS/Linux
      rm -rf node_modules
      
    • 删除锁文件:
      rm package-lock.json
      
    • 清理node-gyp编译缓存:
      npx node-gyp clean
      
  2. 安装electron-rebuild工具
    这个工具专门用于为Electron环境重新编译原生Node模块:

    npm install --save-dev electron-rebuild
    
  3. 配置编译脚本
    在package.json的scripts中添加rebuild命令,或者修改postinstall命令:

    "scripts": {
      "start": "electron .",
      "postinstall": "electron-rebuild",
      "rebuild": "electron-rebuild -f -w ffi-napi ref-napi",
      "dist": "electron-builder"
    }
    
  4. 重新安装依赖并执行编译

    npm install
    # 如果postinstall未自动执行,手动运行
    npm run rebuild
    
  5. 验证运行
    执行启动命令测试:

    npm run start
    

额外注意事项

  • 保持package.json中build.asar为false,避免asar打包影响原生模块的路径解析
  • 确认使用的Node.js版本与Electron内置版本匹配(Electron 23对应Node.js 18.12.0),可通过版本管理工具(如nvm)管理Node版本
  • 若问题仍存在,尝试升级ffi-napi到最新兼容版本,或降级Electron到稳定匹配的版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 10:25:38