英文:
electron / usb incompatible versions
问题
我发现了多个与此类似的问题,但似乎没有任何解决方案适用于我。我正在尝试使用 usb 库 (https://www.npmjs.com/package/usb) 来访问 USB 设备,它在命令行测试应用程序中运行得很完美,但一旦加入 Electron,就出现问题。我在 Ubuntu 14 计算机上安装了 Electron(npm install electron --save-dev),并且他们网站上提供的测试应用程序也能正常运行,但当我添加了我的 usbtools.js 模块
const usb = require('usb');
//...
//示例函数
exports.writeBytes = function writeBytes(deviceObj, dataOut, onWriteOk) {
deviceObj._epOut.transfer(dataOut, (err) => {});
};
//...
并且通过内联 JavaScript 从 HTML 页面内调用其导出的函数时,应用程序加载一个空白页面,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)
//更多错误信息...
我理解这是因为 Electron 使用基于 Chromium 的网络浏览器提供其工作框架,该浏览器不支持当前版本的 node-usb 构建。我已经尝试了重新构建,如建议的那样。是否有人遇到过这个问题?我需要使用 Electron,但只要允许我进行低级别控制(例如:选择接口和端点,我不能只是通过串行线发送数据),我可以使用另一个库来访问 USB 设备。
编辑 1:
Node 版本:13.5.0
Npm 版本:6.13.4
USB 库版本:1.6.2(目前最新版本。npm 无法安装 1.6.0 之前的任何版本)
Electron 版本:7.1.7
英文:
I have found multiple questions similar to this but none of the solutions seems to be working for me. I am trying to use usb library (https://www.npmjs.com/package/usb) to access usb devices, and it worked perfectly as long as it was a command-line test application without Electron. I installed Electron on the Ubuntu 14 computer I'm working with npm install electron --save-dev and the test app provided by their website works too, but when I add my usbtools.js module
const usb = require('usb');
//...
//Example function
exports.writeBytes = function writeBytes(deviceObj, dataOut, onWriteOk) {
deviceObj._epOut.transfer(dataOut, (err) => {});
};
//...
and call its exported functions from within the html page (via inline javascript) the app loads a blank page and DevTools disconnects. When i reload the page with F5, DevTools console gives me the following message:
/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
In my understanding, this is caused by the fact that Electron provides its workframe with a Chromium-based web browser which in turn does not support the current build of node-usb. I have already tried rebuilding as suggested.
Has anyone had this problem? I need to use Electron, but I'm free to use another library to access usb devices, as long as it allows me low level control (eg: choosing interface and endpoint, I cannot just send data over the serial line).
EDIT 1:
node version: 13.5.0
npm version: 6.13.4
usb library version: 1.6.2 (latest, at the moment. npm fails to install anything before 1.6.0)
electron version: 7.1.7
答案1
得分: 0
幸运的是,我找到了一个解决方案:
$npm install electron-rebuild --dev
$./node_modules/.bin/electron-rebuild
这似乎有效,不像常规的重建。
英文:
Luckily, I have found a solution:
$npm install electron-rebuild --dev
$./node_modules/.bin/electron-rebuild
This seems to be working, unlike the regular rebuild.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论