How to run Node.js that is bundled with Electron? - node.js

I'm using the better-sqlite3 NPM module which uses a native extension to talk to SQLite from an Electron app.
In order for this to work with Electron, I need to rebuild the native extension so it is compiled with the same version as the Electron app.
"postinstall": "electron-rebuild -f -w better-sqlite3",
However, now if I want to write a Node.js CLI tool that ships with the application, it's going to use the globally installed Node.js which might be the wrong version:
Error: The module '/Users/chet/Code/brain-web/object-system/node_modules/better-sqlite3/build/Release/better_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 85. This version of Node.js requires
NODE_MODULE_VERSION 72. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
So my question is how do I run node, but specifically reference the Node.js binary that is shipped with electron?
Edit 1: It looks like you can use ./node_modules/.bin/electron index.js to run any Node.js script. However, the application doesn't exit gracefully and errors don't surface in the CLI.

Related

When you start Electron, you get an error that the version of Node.js is not supported

When you start Electron, you get an error that the version of Node.js is not supported.
The operating system is Windows.
When I update Node.js and do node -v, it shows v16.17.1.
When I start it with npm run electron:serve, the following error appears again.
What could be the cause?
The following modules are used to start Electron.
"vue-cli-plugin-electron-builder".
You are running Node.js 12.14.1.
Playwright requires Node.js 14 or higher.
Please update your version of Node.js.

Module compiled against a different NODE_MODULE_VERSION when developing an extension for vscode

I'm trying to use node's tree-sitter package in my vscode extension, but I get the following error:
Activating extension 'extension name' failed: The module '.../node_modules/tree-sitter/build/Release/tree_sitter_runtime_binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 89. Please try re-compiling or re-installing
the module (for instance, using npm rebuild or npm install)..
From what I understand, the NODE_MODULE_VERSION is the version of node's ABI. However, I couldn't even find a release of node that has NODE_MODULE_VERSION 89 in the official website.
What I've tried:
Deleting the node_modules folder and reinstalling the packages.
Running npm rebuild tree-sitter --update-binary from the top directory.
Rebuilding the tree-sitter package with node-gyp rebuild and node-gyp rebuild --target=(my node version) from the node_modules/tree-sitter directory.
Switching node versions using nvm.
None of that helped. I understand from here that changing node versions won't help, as I confirmed when I tried
console.log(process.version); // v14.16.0
console.log(process.versions.modules); // 89
This gave the same output no matter which node version I used. I also tried rebuilding the tree-sitter package using that node version node-gyp rebuild --target=14.16.0, but I get the same error, however this time it says the module was compiled using NODE_MODULE_VERSION 83, which is consistent with what node's site says.
How do I resolve this error?
Any help is appreciated.
As I suspected, the version of node ABI used by vscode extensions is the ABI version used by vscode's internal electron. According to this source
Native Node.js modules are supported by Electron, but since Electron has a different application binary interface (ABI) from a given Node.js binary (due to differences such as using Chromium's BoringSSL instead of OpenSSL), the native modules you use will need to be recompiled for Electron...
This explains why I couldn't find NODE_MODULE_VERSION 89 in node's site.
Next, I checked what version of electron my build of vscode uses. To do this, I simply checked the package.json that came with vscode (/usr/lib/code/package.json on linux, I guess that it is inside the folder vscode is installed on on windows).
Next, following the instructions from electron's site, rebuild the module using the package electron-rebuild. To specify a target version, simply run
./node_modules/.bin/electron-rebuild -v [version]
However, I have no source for this but it seems that tree-sitter does not currently support newer versions of electron, so the build fails. This seems to be because of a change in V8's API (according to this).
The author linked his solution here. I copied his changes and the build succeeded.
Note that I had to replace the existing node addon with the newly built one.

How to run NPM using a specific Node interpreter?

I have an Electron project that uses native libraries, some of which have prebuilds that get downloaded during npm install. This grabs the prebuilds for the global Node version running NPM, which isn't quite what I need. Is there a way to execute NPM explicitly using the Electron binary in my project?

node printer.node is not a valid win32 application

I have developed a node API for my angular application, My node application uses node-printer package for printing pdf files generated by node, when i tried to run my application using nodemon i am getting an error
node printer.node is not a valid win32 application
The same application is working on the other machine without any error. both the machines are of X64 bit architecture.
also i have also tried to install node js 32 bit then too i am getting same error.
This worked for me, installing printer as such:
npm install printer --build-from-source
After so many attempts i have deleted node_modules folder from an application and fire an npm install command to add all modules again that resolves the problem.
Hi there let me go through what's happened on my site here in 2022.
So if you're going to use npm install printer for your Electron application, you're using a native module as deemed by electron.
Since Electron has a different application binary interface (ABI) from a given Node.js binary that you used to install your printer . Therefore, we need to rebuild the native module (in this case printer) for Electron.
For more detail check this out.
First, install electron-rebuild for your project. npm i electron-rebuild.
Second, install a native module using e.g. npm i printer
Third, after installed native module, execute ./node_modules/.bin/electron-rebuild to rebuild the native module.

Issues with using sqlite3 with electron on Mac OS

I am trying to include the sqlite3 package into my electron project. I I get this error:
have done postinstall. It runs correctly on nodejs.
node_modules/sqlite3/lib/binding/electron-v4.0-darwin-x64/node_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 69. Please try re-compiling or re-installing
the module (for instance, using npm rebuild or npm install).
I have the same issue with using sqlite on my developing app, The problem comes from Electron that's change the NODE_MODULE_VERSION to 69 refer to githup issue page to find more info https://github.com/electron/electron/pull/16687
I don't find a really working way to solve the problem for now just downgrade the electron and sqlite version to an older version should solve the problem and build the native module.

Resources