I am making a function for users to transfer data to USB and I am using 'usb' package for it.
If I do const devices = getDeviceList();, then I get all devices info connected through USB such as mouse, and keyboard. However, I should specify only USB mass storage. In this case, how can I specify it? Any suggestion is always welcome and thank you in advance!
FYI: I tried to specify it, but console.log("usb device is: ", usbDevice); gives me null
This is my code:
import {usb, getDeviceList} from "usb";
import fs from "fs";
import path from "path";
async function path2usb(selectedMeasures) {
// Assuming selectedMeasures is an array of data to be exported
try {
usb.on("attach", function (device) {
console.log("USB device attached:");
console.log(device);
});
usb.on("detach", function (device) {
console.log("USB device detached:");
console.log(device);
});
// Get a list of USB devices
const devices = getDeviceList();
console.log("devices are: ", devices);
// Find the first USB mass storage device (e.g., a USB stick)
let usbDevice = null;
for (const device of devices) {
const descriptor = device.deviceDescriptor;
if (
descriptor &&
descriptor.bDeviceClass === 0x08 && // USB mass storage class
descriptor.bDeviceSubClass === 0x06 && // USB mass storage subclass
(descriptor.bDeviceProtocol === 0x50 || descriptor.bDeviceProtocol === 0x01) // Bulk-only transport or SCSI transparent command set
) {
usbDevice = device;
break; // Stop iterating once the USB stick is found
}
}
console.log("usb device is: ", usbDevice);
if (usbDevice) {
usbDevice.open();
const usbInterface = usbDevice.interface(0);
usbInterface.claim();
const endpoint = usbInterface.endpoint(0x81); // Assuming the endpoint is 0x81, you may need to check the endpoint address for your specific USB device.
if (endpoint) {
const folderName = selectedMeasures.join("_"); // Combine the selected measures to form the folder name
const targetFolderPath = path.join("/data", folderName); // Create the target folder path in the 'data' directory
// Check if the folder exists
if (!fs.existsSync(targetFolderPath)) {
throw new Error("Folder not found in 'data' directory.");
}
const targetFilePath = path.join(
`/dev/${endpoint.deviceDescriptor.iSerialNumber}`,
`${folderName}.txt`
); // Create the target file path in the USB mass storage device
// Write the exported data to the file
fs.writeFileSync(targetFilePath, selectedMeasures.join("\n"));
console.log(`Data exported to USB-drive: ${targetFilePath}`);
return targetFilePath;
} else {
// If the endpoint is not found, throw an error
throw new Error("USB endpoint not found!");
}
} else {
// If no USB mass storage device is found, throw an error
throw new Error("No USB Mass Storage device found!");
}
} catch (error) {
console.error("Error exporting data to USB drive:", error.message);
throw error;
}
}
export { path2usb };


Since you seem to be using Linux I would suggest that you look at the symbolic links in
/dev/disk/by-label. You can then use libudev to look at the parents of those devices and see if any of the parents is a USB device. The library libudev is a widely-used C library on Linux, and there is probably a node.js binding for it that would for you.As a proof of concept, before you write any code, just run
ls -l /dev/disk/by-label, and then runudevadm info --attribute-walk /dev/disk/by-label/NAME(whereNAMEis some name you saw in the directory listing). Look forusbin the output of that command.