百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

node-usb-detection

> 编程语言
开源

列出系统中的 USB 设备并检测它们的变化。

373 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

列出系统中的 USB 设备并检测它们的变化。

Deprecated

This library is now deprecated. We recommend using usb for hotplug detection instead. It supports a wider range of platforms, using a more proven codebase.

For help migrating, refer to the documentation for usb


usb-detection

usb-detection allows you to listen for insert/remove events of USB devices on your system.

Changelog

Install

bash
npm install usb-detection

Install for Electron

This module uses native extensions and needs to be compiled for your target version of Electron. Precompiled binaries for recent Node.js and Electron versions are built and published using prebuild and can be installed automatically using electron-rebuild.

See the Electron docs for using native modules to ensure your project is set up to correctly use the prebuilt binaries for your version of Electron.


If you run into the following error, here are the exact steps you can use:

detection.node was compiled against a different Node.js version using NODE_MODULE_VERSION 72. This version of Node.js requires NODE_MODULE_VERSION 80. Please try re-compiling or re-installing
  1. npm i electron-rebuild --save-dev
  2. ./node_modules/.bin/electron-rebuild

Usage

…

API

usbDetect.startMonitoring()

Start listening for USB add/remove/change events. This will cause the Node.js process to stay open until you call usbDetect.stopMonitoring() (see below).

usbDetect.stopMonitoring()

Stop listening for USB add/remove/change events. This will also allow the Node.js process to exit.

This is really only meant to be called once on exit. No guarantees if you start/stop monitoring multiple times, see https://github.com/MadLittleMods/node-usb-detection/issues/53

usbDetect.on(eventName, callback)

  • eventName
    • add: also aliased as insert
      • add:vid
      • add:vid:pid
    • remove
      • remove:vid
      • remove:vid:pid
    • change
      • change:vid
      • change:vid:pid
  • callback: Function that is called whenever the event occurs
    • Takes a device
javascript
var usbDetect = require('usb-detection');
usbDetect.startMonitoring();

usbDetect.on('add', function(device) {
	console.log(device);
});

/* Console output:
{
	locationId: 0,
	vendorId: 5824,
	productId: 1155,
	deviceName: 'Teensy USB Serial (COM3)',
	manufacturer: 'PJRC.COM, LLC.',
	serialNumber: '',
	deviceAddress: 11
}
*/

usbDetect.find(vid, pid, callback)

Note: All find calls return a promise even with the node-style callback flavors.

  • find()
  • find(vid)
  • find(vid, pid)
  • find(callback)
  • find(vid, callback)
  • find(vid, pid, callback)

Parameters:

  • vid: restrict search to a certain vendor id
  • pid: restrict search to s certain product id
  • callback: Function that is called whenever the event occurs
    • Takes a err and devices parameter.
…

FAQ

The script/process is not exiting/quiting

javascript
var usbDetect = require('usb-detection');

// Do some detection
usbDetect.startMonitoring();

// After this call, the process will be able to quit
usbDetect.stopMonitoring();

usbDetect.find() always returns the same list of devices, even after removal.

Make sure you call usbDetect.startMonitoring() before any calls to usbDetect.find().

npm run rebuild -> The system cannot find the path specified.

If you are running into the The system cannot find the path specified. error when running npm run rebuild, make sure you have Python installed and on your PATH.

You can verify node-gyp is configured correctly by looking at the output of node-gyp configure --verbose.

To build a debug version with error outputs use:

bash
$ npm run rebuild --debug

Development (compile from source)

This assumes you also have everything on your system necessary to compile ANY native module for Node.js using node-gyp. This may not be the case, though, so please ensure the following requirements are satisfied before filing an issue about "does not install".

If you are developing locally, you should use Node.js v14, but if you are just trying to install usb-detection, you should be able to compile from source using any supported version of Node.

Windows

See node-gyp's Windows installation instructions.

macOS

See node-gyp's macOS installation instructions.

Linux

You know what you need for you system, basically your appropriate analog of build-essential. Keep rocking! See node-gyp's Unix installation instructions for more details.

bash
sudo apt-get install build-essential

You will also need to install libudev-dev.

bash
sudo apt-get install libudev-dev

Testing

We have a suite of Mocha/Chai tests.

The tests require some manual interaction of plugging/unplugging a USB device. Follow the cyan background text instructions.

bash
npm test

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言