Вы можете использовать модуль drivelist , чтобы получить список USB-накопителей на событии вставки / извлечения модуля USB-обнаружения и принять разность. Это хакерский способ, который я написал пару недель назад . Написание собственного модуля узла может быть лучшим способом сделать это.
const usbDetect = require('usb-detection');
const drivelist = require('drivelist');
let usbList = [];
usbDetect.startMonitoring();
// Detect insert
usbDetect.on('add', () => {
const poll = setInterval(() => {
drivelist.list().then((drives) => {
drives.forEach((drive) => {
if (drive.isUSB) {
const mountPath = drive.mountpoints[0].path;
if (!usbList.includes(mountPath)) {
console.log(mountPath); //op
usbList.push(mountPath);
clearInterval(poll)
}
}
})
})
}, 2000)
});
// Detect remove
usbDetect.on('remove', () => {
let newUsbList = []
let removalList = []
drivelist.list().then((drives) => {
drives.forEach((drive) => {
if (drive.isUSB) {
newUsbList.push(drive.mountpoints[0].path);
}
})
removalList = usbList.filter(x => !newUsbList.includes(x));
usbList = usbList.filter(x => !removalList.includes(x))
console.log(removalList) // op
})
});
//usbDetect.stopMonitoring()