electron / usb 不兼容版本

electron / usb incompatible versions

我发现了多个与此类似的问题,但 none 的解决方案似乎对我有用。我正在尝试使用 usb 库(https://www.npmjs.com/package/usb)来访问 USB 设备,只要它是一个命令,它就可以正常工作- 没有 Electron 的在线测试应用程序。我在 Ubuntu 14 计算机上安装了 Electron 我正在使用 npm install electron --save-dev 并且他们网站提供的测试应用程序也可以工作,但是当我添加我的 usbtools.js 模块

const usb = require('usb');
//...
//Example function
exports.writeBytes = function writeBytes(deviceObj, dataOut, onWriteOk) {
  deviceObj._epOut.transfer(dataOut, (err) => {});
};
//...

并从 html 页面中调用其导出的函数(通过内联 javascript)应用加载空白页面并且 DevTools 断开连接。当我使用 F5 重新加载页面时,DevTools 控制台给我以下消息:

/home/carl/prstatjs_dist/node_modules/bindings/bindings.js:121 Uncaught Error: The module '/home/carl/prstatjs_dist/node_modules/usb/build/Release/usb_bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 79. This version of Node.js requires
NODE_MODULE_VERSION 75. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func (electron/js2c/asar.js:140)
    at process.func [as dlopen] (electron/js2c/asar.js:140)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:922)
    at Object.func (electron/js2c/asar.js:140)
    at Object.func [as .node] (electron/js2c/asar.js:140)
    at Module.load (internal/modules/cjs/loader.js:735)
    at Module._load (internal/modules/cjs/loader.js:648)
    at Module._load (electron/js2c/asar.js:717)
    at Function.Module._load (electron/js2c/asar.js:717)
    at Module.require (internal/modules/cjs/loader.js:775)
func @ electron/js2c/asar.js:140
func @ electron/js2c/asar.js:140
Module._extensions..node @ internal/modules/cjs/loader.js:922
func @ electron/js2c/asar.js:140
func @ electron/js2c/asar.js:140
Module.load @ internal/modules/cjs/loader.js:735
Module._load @ internal/modules/cjs/loader.js:648
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.require @ internal/modules/cjs/loader.js:775
require @ internal/modules/cjs/helpers.js:68
bindings @ /home/carl/prstatjs_dist/node_modules/bindings/bindings.js:112
(anonymous) @ /home/carl/prstatjs_dist/node_modules/usb/usb.js:1
(anonymous) @ /home/carl/prstatjs_dist/node_modules/usb/usb.js:526
Module._compile @ internal/modules/cjs/loader.js:880
Module._extensions..js @ internal/modules/cjs/loader.js:892
Module.load @ internal/modules/cjs/loader.js:735
Module._load @ internal/modules/cjs/loader.js:648
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.require @ internal/modules/cjs/loader.js:775
require @ internal/modules/cjs/helpers.js:68
(anonymous) @ /home/carl/prstatjs_dist/prstat/usbtools.js:1
(anonymous) @ /home/carl/prstatjs_dist/prstat/usbtools.js:319
Module._compile @ internal/modules/cjs/loader.js:880
Module._extensions..js @ internal/modules/cjs/loader.js:892
Module.load @ internal/modules/cjs/loader.js:735
Module._load @ internal/modules/cjs/loader.js:648
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.require @ internal/modules/cjs/loader.js:775
require @ internal/modules/cjs/helpers.js:68
(anonymous) @ /home/carl/prstatjs_dist/prstat/stampanti.js:3
(anonymous) @ /home/carl/prstatjs_dist/prstat/stampanti.js:354
Module._compile @ internal/modules/cjs/loader.js:880
Module._extensions..js @ internal/modules/cjs/loader.js:892
Module.load @ internal/modules/cjs/loader.js:735
Module._load @ internal/modules/cjs/loader.js:648
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.require @ internal/modules/cjs/loader.js:775
require @ internal/modules/cjs/helpers.js:68
(anonymous) @ /home/carl/prstatjs_dist/prstat/prstat.js:2
(anonymous) @ /home/carl/prstatjs_dist/prstat/prstat.js:385
Module._compile @ internal/modules/cjs/loader.js:880
Module._extensions..js @ internal/modules/cjs/loader.js:892
Module.load @ internal/modules/cjs/loader.js:735
Module._load @ internal/modules/cjs/loader.js:648
Module._load @ electron/js2c/asar.js:717
Module._load @ electron/js2c/asar.js:717
Module.require @ internal/modules/cjs/loader.js:775
require @ internal/modules/cjs/helpers.js:68
(anonymous) @ esempio_electron_codice.js:3
Show 23 more frames

据我了解,这是由于 Electron 为其工作框架提供了基于 Chromium 的 Web 浏览器,而该浏览器不支持当前构建的 node-usb。我已经尝试按照建议进行重建。 有人遇到过这个问题吗?我需要使用 Electron,但我可以自由使用另一个库来访问 USB 设备,只要它允许我进行低级控制(例如:选择接口和端点,我不能只通过串行线路发送数据)。

编辑 1:
节点版本:13.5.0
npm 版本:6.13.4
usb库版本:1.6.2(目前最新。npm无法安装1.6.0之前的任何东西)
电子版本:7.1.7

幸运的是,我找到了解决方案:

$npm install electron-rebuild --dev
$./node_modules/.bin/electron-rebuild

这似乎有效,与常规重建不同。