【问题标题】:Why is Nodejs usb module conflicting with Electron?为什么 Nodejs usb 模块与 Electron 冲突?
【发布时间】:2021-01-01 22:55:26
【问题描述】:

抱歉,这篇文章很长,我一直在谷歌上搜索很多解决方案,但找不到。我正在尝试编写一个简单的 Electron 应用程序来显示一个 html 页面(幸好没问题)并控制打印机,这必须通过通过 npm 安装的 usb 模块来完成。该模块和依赖它的代码总是在纯 node.js 软件中完美运行,所以这不是寻找问题的地方,但我很难将它集成到我的 Electron 应用程序中。 我必须使用 Ubuntu 14.04,并且系统已经有 node 12.20.0 + npm 6.14.8,两者都是通过 nvm 安装的,没有任何警告(我可以安装不同的版本,如果有任何已知的冲突)。由于使用通过 npm 安装的 Electron 存在一系列困难,我求助于使用它的预构建二进制包(版本 11.1.1,但如果需要,我可以再次更改),可以从https://github.com/electron/electron/releases/tag/v11.1.1

这是我的工作:

  • 解压包
  • 转到 [文件夹]/resources/app 并将我的源代码 + 默认 package.json 放在那里
  • 安装usb模块1.6.3:$npm install usb --save-dev
  • 返回主目录并运行应用程序:$./electron

这是我得到的:

./electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by ./electron)
/home/totem/totem/electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by /home/totem/totem/electron)
/home/totem/totem/electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by /home/totem/totem/electron)
Fontconfig warning: "/etc/fonts/fonts.conf", line 86: unknown element "blank"
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The module '/home/totem/totem/resources/app/node_modules/usb/build/Release/usb_bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 85. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (electron/js2c/asar_bundle.js:5:1812)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1203:18)
    at Object.func [as .node] (electron/js2c/asar_bundle.js:5:1812)
    at Module.load (internal/modules/cjs/loader.js:992:32)
    at Module._load (internal/modules/cjs/loader.js:885:14)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12738)
    at Module.require (internal/modules/cjs/loader.js:1032:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at bindings (/home/totem/totem/resources/app/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/home/totem/totem/resources/app/node_modules/usb/usb.js:1:57)
Segmentation fault
  • 好的,这可以理解,因为 usb 是原生模块,必须重新构建。
  • 返回[文件夹]/resources/app
  • $npm install electron-rebuild --dev
  • 如果需要,我会提供大量输出,但都以 found 0 漏洞结束,所以我认为没问题
  • $./node_modules/.bin/electron-rebuild
An unhandled error occurred inside electron-rebuild
Unable to find electron's version number, either install it or specify an explicit version

Error: Unable to find electron's version number, either install it or specify an explicit version
    at /home/totem/totem/resources/app/node_modules/electron-rebuild/lib/src/cli.js:82:19
  • 不知道为什么,但很容易解决:$./node_modules/.bin/electron-rebuild --version 11.1.1
  • 重建完成:太好了,我们回到主目录
  • 尝试运行应用程序:$./electron
./electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by ./electron)
/home/totem/totem/electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by /home/totem/totem/electron)
/home/totem/totem/electron: /usr/lib/x86_64-linux-gnu/libxkbcommon.so.0: no version information available (required by /home/totem/totem/electron)
Fontconfig warning: "/etc/fonts/fonts.conf", line 86: unknown element "blank"
A JavaScript error occurred in the main process
Uncaught Exception:
Error: The module '/home/totem/totem/resources/app/node_modules/usb/build/Release/usb_bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 85. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (electron/js2c/asar_bundle.js:5:1812)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1203:18)
    at Object.func [as .node] (electron/js2c/asar_bundle.js:5:1812)
    at Module.load (internal/modules/cjs/loader.js:992:32)
    at Module._load (internal/modules/cjs/loader.js:885:14)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12738)
    at Module.require (internal/modules/cjs/loader.js:1032:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at bindings (/home/totem/totem/resources/app/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/home/totem/totem/resources/app/node_modules/usb/usb.js:1:57)

发生了什么事?就像电子重建根本没有做任何事情一样。我可以更改除操作系统之外的所有内容,并且(除非没有其他解决方案可用)我在 $npm install electron 上使用 Electron 预构建包这一事实。 知道有什么问题吗? 谢谢大家的关注。

【问题讨论】:

    标签: javascript node.js linux npm electron


    【解决方案1】:

    你必须重建任何

    npm rebuild
    # or
    npm install electron-rebuild --save-dev
    ./node_modules/.bin/electron-rebuild
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2020-02-12
      • 1970-01-01
      相关资源
      最近更新 更多