首页 > 解决方案 > Electron 无法从 node_modules 文件夹打开共享对象文件

问题描述

在 electron 上,节点模块vosk需要访问一些位于node_modules/vosk/lib/.

我现在遇到的问题是,当我这样做require('vosk')main.js尝试执行我的 AppImage 文件时,我得到:

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Dynamic Linking Error: /tmp/.mount_CantooClaxGf/resources/app.asar/node_modules/vosk/lib/linux-x86_64/libvosk.so: Cannot open the shared object: It's not a folder
    at new DynamicLibrary (/tmp/.mount_CantooClaxGf/resources/app.asar/node_modules/ffi-napi/lib/dynamic_library.js:75:11)
    at Object.Library (/tmp/.mount_CantooClaxGf/resources/app.asar/node_modules/ffi-napi/lib/library.js:47:10)
    at Object.<anonymous> (/tmp/.mount_CantooClaxGf/resources/app.asar/node_modules/vosk/index.js:24:21)
    at Module._compile (internal/modules/cjs/loader.js:1145:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1166:10)
    at Module.load (internal/modules/cjs/loader.js:981:32)
    at Module._load (internal/modules/cjs/loader.js:881:14)
    at Function.Module._load (electron/js2c/asar.js:769:28)
    at Module.require (internal/modules/cjs/loader.js:1023:19)
    at require (internal/modules/cjs/helpers.js:77:18)

我尝试将 vosk 添加到构建中的文件中:

 "build": {
    "files": [
      "dist/**/*",
      "src/assets/icons/*",
      "electron.js",
      "package.json",
      "assets/models/*",
      "node_modules/vosk/lib/*"
    ],

我现在可以看到文件app.asar.unpacked/node_modules/vosk/lib/夹中的文件,但是在执行应用程序时,我仍然遇到同样的错误。

我发现这个答案提到了一个黑客,但它没有解决我的问题,我仍然有完全相同的错误。

我应该如何以vosk可以找到它们的方式打包共享对象?

标签: node.jselectronvosk

解决方案


我可以用这个电子配置解决问题,将 vosk 的所有依赖项放在 extraResources 字段中:

  "build": {
    "extraResources": [
      "node_modules/at-least-node/**/*",
      "node_modules/builder-util-runtime/**/*",
      "node_modules/debug/**/*",
      "node_modules/ffi-napi/**/*",
      "node_modules/fs-extra/**/*",
      "node_modules/get-symbol-from-current-process-h/**/*",
      "node_modules/get-uv-event-loop-napi-h/**/*",
      "node_modules/graceful-fs/**/*",
      "node_modules/jsonfile/**/*",
      "node_modules/ms/**/*",
      "node_modules/node-addon-api/**/*",
      "node_modules/node-gyp-build/**/*",
      "node_modules/ref-napi/**/*",
      "node_modules/ref-struct-di/**/*",
      "node_modules/sax/**/*",
      "node_modules/universalify/**/*",
      "assets/models/**/*"
    ],
    "files": [
      "dist/**/*",
      "src/assets/icons/*",
      "electron.js",
      "package.json"
    ],

我还需要这个库

它现在按预期工作


推荐阅读