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

1

7

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?

Liquate answered 17/4, 2020 at 21:29 Comment(5)
Your native module must be compiled using the same version of node that VS Code's Electron framework uses. So, that also means, you must upgrade your node module each and every time VS Code changes the version of Electron framework. So when compiling your native module, make sure the node-gyp uses the correct Node version that the Electron framework uses. This might answer your question: #46385091Embellish
Thanks for the feedback... I am having trouble finding info about the actual steps to do what you suggest mostly because I don't know much about electron. I have only ever created a couple of VS Code extensions, no full electron apps. I have been going entirely by the VS Code docs so I am not aware of the internals and there is not much in the VS Code docs. How do I tell node-gyp which version to use? I think I want node module version 75, right? I have electron-rebuild but can't seem to tell it how to rebuild with version 75. Any help or pointers to docs would be appreciated!Liquate
Did you try what the error message suggested? i.e. Rebuilding or reinstalling the problematic module?Principe
Yes, I tried npm rebuild and reinstalling. I get the same error message. The problem seems to be that the version of node I use for non-vscode extension development isn't the same as the one required for vscode extensions. I do not know how to change the target of my current node install so that it aligns with vscode extension development. That is my best guess anyway.Liquate
Allows you to easily change your node version on a per-project basis: github.com/nvm-sh/nvmBini
C
5

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

Culosio answered 25/4, 2020 at 6:13 Comment(5)
OP, kindly mark the answer as correct if it worked for you :)Culosio
I have created the VS Code extension using the Yeoman project generator. When I do as you suggested, use the electron-rebuild command, it tells me, "An unhandled error occurred inside electron-rebuild. Unable to find electron's version number, either install it or specify an explicit version" However, I can't find the version in package.json or package-lock.json so I don't know what version to use.Liquate
Are there chances that you don't have electron installed at all in the first place? Is your problem solved yet?Culosio
As you can tell I am not too knowledgeable on electron. I can tell you that my VS Code extension runs fine without the module that is giving me the problem. So, maybe it is not an electron issue and more of a node issue??Liquate
You can raise this as an issue inside the github issue tracker of the module that is raising the issue. They might help you better or tell you how to compile with the correct version as expectedCulosio

© 2022 - 2024 — McMap. All rights reserved.