Node.js implementation of the WebUSB Specification
Node.js > v8.14.0, which includes npm
.
$ npm install webusb
The module exports a default navigator.usb
instance and the USB
class to allow you to instantiate your own usb instances:
To use existing WebUSB scripts, you can simply use the default usb
instance in place of the navigator.usb
object:
const usb = require("webusb").usb;
const device = usb.requestDevice({
filters: [{vendorId: 0x0d28}]
});
...
The first device matching the filters will be returned.
You may want to create your own instance of the USB
class. For example, to inject a device chooser function:
const USB = require("webusb").USB;
const devicesFound = async devices => {
// If one of the devices can be automatically selected, you can return it
for (const device of devices) {
if (device.productName === "myName") return device;
}
// Otherwise return a deferred Promise and resolve it later with a device
}
const usb = new USB({
devicesFound
});
const device = usb.requestDevice({
filters: [{vendorId: 0x0d28}]
});
...
The WebUSB specification can be found here: