Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn a node app into a VS Code extension?

I have built a node app that uses a module to work with a database (better-sqlite3).

It works fine as a node app. but, Now I am trying to make a VS Code extension that has most of the same functionality.

However, when I install the module, build, and run the new extension I get this message:

Activating extension 'undefined_publisher.myPlugin' failed: The module '\?\C:...\node_modules\better-sqlite3\build\Release\better_sqlite3.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 72. This version of Node.js requires NODE_MODULE_VERSION 75. Please try re-compiling or re-installing the module (for instance, using npm rebuild or npm install)..

I understand that VS Code extensions are Electron apps and they use a different version of the node then the one I created for my pure node app but I am unclear what I have to do in the extension to build the module (better-sqlite3) with the correct version of node?

like image 368
Mark M Avatar asked Apr 17 '20 21:04

Mark M


1 Answers

npm rebuild compiles the code under plain node. It will not build addons. To resolve, you have to do the following:

npm install --save-dev electron-rebuild

# Every time you run "npm install", run this:
./node_modules/.bin/electron-rebuild

# On Windows if you have trouble, try:
.\node_modules\.bin\electron-rebuild.cmd

Also, if the above doesn't work, you have some cleaning and rebuilding to do, which is as follows:

  • If you have not installed electron-rebuild just install it with the command: npm i -D electron-rebuild
  • Remove from the node-modules folder the <your-module-name> and @<your-module-name> folders.
  • Remove the file packages-lock.json
  • Run npm i to install non-installed modules
  • And finally run ./node_modules/.bin/electron-rebuild or corresponding windows equivalent - .\node_modules\.bin\electron-rebuild.cmd It is very important to run the above command directly after npm i.

Reference: Electron docs - https://www.electronjs.org/docs/tutorial/using-native-node-modules

Similar issue - https://github.com/mscdex/cap/issues/92

Similar issue - https://github.com/serialport/node-serialport/issues/1910

like image 84
Udaya Prakash Avatar answered Sep 28 '22 08:09

Udaya Prakash