ffi-napi运行失败,报错:No native build was found,解决办法

ffi-napi运行失败:No native build was found,报错如下:

javascript 复制代码
renderer.dev.js:113102 Uncaught Error: No native build was found for platform=win32 arch=x64 runtime=electron abi=76 uv=1 libc=glibc
    at Function.module.exports../node_modules/node-gyp-build/index.js.load.path (renderer.dev.js:113102)
    at load (renderer.dev.js:113067)
    at Object../node_modules/ref-napi/lib/ref.js (renderer.dev.js:184979)
    at __webpack_require__ (renderer.dev.js:771)
    at fn (renderer.dev.js:131)
    at Object../node_modules/ffi-napi/lib/ffi.js (renderer.dev.js:89883)
    at __webpack_require__ (renderer.dev.js:771)
    at fn (renderer.dev.js:131)
    at Module.<anonymous> (renderer.dev.js:26630)
    at Module../app/services/webdav-io.ts (renderer.dev.js:27293)

解决办法:这是由于在build的时候找不到可运行的.node文件导致的,需要在webpack手动加入node文件,一般文件命名为webpack.config.js ,参考代码如下,也就是要给pluginsexternals里加上ffi_napi :

javascript 复制代码
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
// 给后端配置追加 CopyWebpackPlugin,复制 .node 文件到正确路径
nodeConfig.config.plugins.push(
    new CopyWebpackPlugin({
        patterns: [
            // 复制 ref-napi 的 .node 文件到 lib/backend/native/ 下(匹配 Theia 的 node-loader 配置)
            {
                from: path.resolve(__dirname, '../../node_modules/ref-napi/build/Release/ref_napi.node'),
                to: path.resolve(__dirname, 'lib/backend/native/ref_napi.node'),
                force: true // 强制覆盖已有文件
            },
            // 复制 ffi-napi 的 .node 文件
            {
                from: path.resolve(__dirname, '../../node_modules/@napi-ffi/ffi-napi/build/Release/ffi_napi.node'),
                to: path.resolve(__dirname, 'lib/backend/native/ffi_napi.node'),
                force: true
            }
        ]
    })
);

// 给后端配置追加 externals,排除原生模块打包
nodeConfig.config.externals = {
    ...nodeConfig.config.externals, // 保留原有 externals
    "ref-napi": "commonjs ref-napi",
    "@napi-ffi/ffi-napi": "commonjs @napi-ffi/ffi-napi"
};

然后重新构建,可以看到成功不报错了:

如果是在之后Electron使用过程中遇到了External buffers are not allowed 报错问题,请参考我的这篇文章解决:https://blog.csdn.net/changyana/article/details/156726286

相关推荐
Wenweno0o19 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming66619 小时前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
cch891820 小时前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳20 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发20 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense20 小时前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎20 小时前
STL 栈 队列
开发语言·c++
勿忘,瞬间20 小时前
数据结构—顺序表
java·开发语言
张張40820 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_4235339920 小时前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python