1

I write an Electron app that uses nodegit. For my test part I use ava in combination with Spectron to test my app. All of my tests work - including functions which use nodegit in my app.

In addition to the tests described above I made also a pure non-Electron test file in which I import nodegit directly.

 import * as nodegit from 'nodegit';

Executing this test now via ava returns this:

node_modules\.pnpm\nodegit@0.27.0\node_modules\nodegit\build\Release\nodegit.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 82. This version of Node.js requires
NODE_MODULE_VERSION 83. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Module._extensions..node (internal/modules/cjs/loader.js:1122:18)

Where exactly does version 82 come from? I only have nodejs 14.15.0 installed, which uses version 83 as expected. Why does node think the version is a mismatch where it works actually in my app? This is how my package.json looks like:

  "devDependencies": {
    "ava": "^3.13.0",
  },
  "scripts": {
    "ava": "node_modules/.bin/ava",
     ...
  },
  "ava": {
    "files": [
      "*.ts"
    ],
    "extensions": [
      "ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "nodeArguments": [
      "--napi-modules",
      "--experimental-modules"
    ]
  },

I built nodegit myself and in the config.gypi file it even refers to:

    "node_module_version": 83,

I made a super simple reproducible example: https://github.com/Githubber2021/node_module_version-issue

% node --version
14.15.0
% npm install
% npm run ava
... error

Can anyone explain me if this a bug or where version 82 comes from?

Daniel Stephens
  • 596
  • 4
  • 14
  • 39
  • https://stackoverflow.com/questions/63794519/serialport-for-electron-node-module-version-error-and-rebuild-does-not-fix – Robert Nov 13 '20 at 01:35
  • Thanks! That actually helped but didn't solve the problem. I think this issue is caused by the fact that Electron and NodeJS have different NODE_MODULE_VERSIONS even if I use an Electron version which uses the same nodejs version. I had to create a follow up question for this: https://stackoverflow.com/questions/64815484/why-is-there-a-mismatch-of-module-versions-between-electron-and-node-js – Daniel Stephens Nov 13 '20 at 04:29

3 Answers3

1

Hey) I think I can solve your problem, just try this:

"engines": {
  "node": ">=14.0.0"
},

to your package.json and remove node_modules and do npm i in your project directory. It should help

Pavel
  • 125
  • 1
  • 1
  • Unfortunately that wouldn't fix it. I noticed that the standard `node` and the node version of Electron have different ABI versions, so they will never be compatible :-/ – Daniel Stephens Nov 15 '20 at 05:49
1

This could be one of two things:

  1. The native dependency you're loading provides prebuilt binaries via prebuild -- This is probably the case
  2. The native dependency you're loading is downloaded as source code and building the binary is up to you.

According to nodegit's README,

"NodeGit will work on most systems out-of-the-box without any native dependencies."

But it looks like you need at minimum nodegit@0.27.x to get prebuilt binaries from use Node 14. Source

So the 82 you're seeing comes from the ABI version that the prebuilt binary was compiled against. Since you're already using nodegit@0.27.x, then somehow you've ended up with the prebuilt binaries for the wrong ABI.

Here's a repo I use that lists out all the various binaries for different versions, so you can see how this might happen: https://github.com/lovell/sharp/tree/v0.25.3

I develop in Electron and for Electron I use a command like this to get the correct version of Electron to run npm rebuild against.

npm rebuild --runtime=electron --target=8.5.3 --disturl=https://atom.io/download/atom-shell

I don't know exactly what the equivalent is for plain node, but let me know if this gets you closer and if so I'll see what I can find.

Slbox
  • 5,695
  • 6
  • 30
  • 70
  • 1
    Thanks! I figured that out but that answer is still amazing and that deservers the points. The ABI of the node version of Electron and the ABI of the system node is different by design and causes the mismatch – Daniel Stephens Nov 19 '20 at 00:06
1

The answer is already solve, just adding one approach.

As mention, the ABI (application binary interface) of your Node.Js could be diff from the ABI of the Node.js used by Electron, regardless of the version of each Node.js.

So you install a native module using NPM that builds OK using the Node.js you have with the corresponding NODE_MODULE_VERSION (ABI), but when Electron try to use it, you get an error asking your module to use the ABI version of the Node.js included in Electron.

The other approach:

Install modules like any other Node project, then rebuild for Electron with the electron-rebuild package.

npm install --save-dev electron-rebuild
./node_modules/.bin/electron-rebuild

Notes

  1. This will rebuild your already build module but this time automatically looking for headers of the Electron version you have.
  2. You need to repeat this every time you run "npm install" (or add the sequence in the script section of your package.json).
  3. Using "Electron Forge", this tool is automatically applied in development or when making distributables. Using "Electron Packager" you may have a look in the project's README.

source: https://github.com/electron/electron-rebuild

caob
  • 26
  • 2