microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.62k stars 28.67k forks source link

How to use node-usb-native in a Extension #164362

Closed geekroscom closed 1 year ago

geekroscom commented 1 year ago

Hello, we are currently working on an extension: https://marketplace.visualstudio.com/items?itemName=GEEKROS.geekstudio

I have encountered some problems at present, about the use of node-usb-native.

The development environment information is as follows:

Node Version: v14.19.3 VSCode Version: 1.72.2

Extension Package.json:

......
"scripts": {
    "install:all": "yarn && cd packages/ProjectPanel && yarn",
    "build:all": "yarn rimraf:all && yarn vscode:compile && cd packages/ProjectPanel && yarn build && cd ../../",
    "rimraf:all": "rimraf release/extension && rimraf release/ProjectPanel && rimraf release/package.json",
    "usb:rebuild": "node ./node_modules/node-usb-native/scripts/rebuild-serialport.js",
    "panel:project": "cd packages/ProjectPanel && yarn start",
    "vscode:prepublish": "yarn vscode:compile",
    "vscode:compile": "tsc -p ./",
    "vscode:watch": "tsc -watch -p ./"
},
"devDependencies": {
    "@types/glob": "^7.1.3",
    "@types/node": "^13.11.0",
    "@types/vscode": "^1.46.0",
    "@typescript-eslint/eslint-plugin": "^4.14.1",
    "@typescript-eslint/parser": "^4.14.1",
    "eslint": "^7.19.0",
    "glob": "^7.1.6",
    "node-gyp": "^6.1.0",
    "prettier": "^2.2.1",
    "typescript": "^4.1.3",
    "vscode-test": "^1.5.0"
},
"dependencies": {
    "node-usb-native": "^0.0.20",
    "opn": "^6.0.0",
    "request": "^2.88.2",
    "rimraf": "^3.0.2",
    "ws": "^7.4.5"
}
......

When the extension is compiled and debugged, an error is reported:

Error: The module '\\?\d:\Geek_Studio_Code\node_modules\usb-detection\build\Release\detection.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 83. This version of Node.js requires
NODE_MODULE_VERSION 106. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func [as dlopen] (node:electron/js2c/asar_bundle:5:1812)
    at Object.<anonymous> (node:internal/modules/cjs/loader:1250:18)
......

In the current Node development environment, the electron program has been developed a long time ago, and usb-detection can be used normally.

I have read and tried a lot of information on github, but cannot solve this problem. Can you give some guidance, methods, and procedures?

geekroscom commented 1 year ago

roblourens commented 1 year ago

I think you need to build your native node module using the same node version/ABI that vscode is on. This might help https://stackoverflow.com/questions/71527006/module-compiled-against-a-different-node-module-version-when-developing-an-exten

geekroscom commented 1 year ago

Hopes to have built-in support for usb detection and serialport! This is conducive to the creation and expansion of hardware developers.

deepak1556 commented 1 year ago

For native module issue in question, you would have to rebuild for the Node ABI version of VSCode engine or your native module can use NAPI to avoid this in general. As for built-in support, have you looked into WebUSB, WebSerial apis which are now supported for web UI exetensions https://code.visualstudio.com/updates/v1_69#_webusb-webserial-and-webhid-access-on-web