加载.node文件报错:no suitable image found. unknown file type, first eight bytes

背景

electron-vue构建的工程在打包的时候报错如下:

Uncaught Error: Cannot open /Users/mac/Desktop/file/zl/git_project/new_client/electron-bjcast-client/build/mac/wirelessCast.app/Contents/Resources/app.asar/dist/electron/bjcast_project_addon.node: Error: dlopen(/var/folders/2j/xhy537wj5731y95lftfkk1fm0000gn/T/.com.bjnet.app.Sn7Dzl, 1): no suitable image found.  Did find:
 /var/folders/2j/xhy537wj5731y95lftfkk1fm0000gn/T/.com.bjnet.app.Sn7Dzl: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00
 /var/folders/2j/xhy537wj5731y95lftfkk1fm0000gn/T/.com.bjnet.app.Sn7Dzl: stat() failed with errno=17
    at Object.<anonymous> (renderer.js:7)
    at Object.<anonymous> (renderer.js:7)
    at f (renderer.js:1)
    at o (renderer.js:1)
    at Module.<anonymous> (renderer.js:12)
    at f (renderer.js:1)
    at renderer.js:1
    at renderer.js:1

为了做到跨平台在程序中加载了bjcast_project_addon.node命名的node文件,具体的引入方式如下:

if (platform === 'darwin') {
  BJLib = require("@/assets/BJSDK/mac/bjcast_project_addon.node");
} else if (platform === 'win32') {
  BJLib = require("@/assets/BJSDK/windows/lib_32/bjcast_project_addon.node");
}

因为electron-vue打包时使用了webpack,webpack会把node文件复制到dist\electron\bjcast_project_addon.node目录下,程序中加载的两个node文件一个是window编译的,一个是macOS编译的,如果名称相同复制之后类型可能无法判断。

解决

mac目录下的bjcast_project_addon.node名称 改为:bjcast_project_addon_mac.node

if (platform === 'darwin') {
  BJLib = require("@/assets/BJSDK/mac/bjcast_project_addon_mac.node");
} else if (platform === 'win32') {
  BJLib = require("@/assets/BJSDK/windows/lib_32/bjcast_project_addon.node");
}

你可能感兴趣的:(electron,vue,nodejs,webpack)