Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to build in Electron #704

Closed
poralcode opened this issue Sep 27, 2021 · 17 comments
Closed

Failed to build in Electron #704

poralcode opened this issue Sep 27, 2021 · 17 comments

Comments

@poralcode
Copy link

Can anybody help me use this in Electron?

I tried the following command,

npm install --save-dev electron-rebuild
npm install --save-dev better-sqlite3
.\node_modules\.bin\electron-rebuild.cmd

but every time I start running the application, I got an error of

..\better_sqlite3.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 83. This version of Node.js requires
NODE_MODULE_VERSION 98. 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:1800)
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1170:18)
    at Object.func [as .node] (node:electron/js2c/asar_bundle:5:1800)
    at Module.load (node:internal/modules/cjs/loader:982:32)
    at Module._load (node:internal/modules/cjs/loader:823:12)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
    at Module.require (node:internal/modules/cjs/loader:1006:19)
    at require (node:internal/modules/cjs/helpers:93:18)
@kryops
Copy link

kryops commented Sep 28, 2021

I ran into a similar problem when upgrading to Electron 15. The problem seems to be that

  1. there are no pre-built binaries for Electron 15 yet (should be solved by add electron 14 and 15 #705)
  2. electron-rebuild does not seem to build from source when it cannot find pre-built binaries

I got it working by rebuilding from source manually:

yarn --cwd .\node_modules\better-sqlite3\ build-release --target=15.0.0 --arch=x64 --dist-url=https://electronjs.org/headers

But I wonder if there is a way to make electron-rebuild automatically build from source when it cannot find pre-built binaries 🤔 (not sure whether this is a problem with better-sqlite3 or with electron-rebuild itself)

@poralcode
Copy link
Author

Something doesn't add up to me. I go home, tried rebuilding from the source manually and it works. Then I go to office, do the same, thinking it will work but guess what? I got the same error. I'm scratching my hair right now.

@tom2strobl
Copy link

@kryops experienced 100% the same as you, manually building works, but it's tedious if electron-rebuild is set up as a postinstall; I have to manually rebuild after every module addition.

NODE_MODULE_VERSION is the abi number, which one can look up here: abi_version_registry eg.:

{ "modules": 98, "runtime": "electron", "variant": "electron",             "versions": "15" },
{ "modules": 89, "runtime": "electron", "variant": "electron",             "versions": "13" },
{ "modules": 83, "runtime": "node",     "variant": "v8_8.1",               "versions": "14.0.0" },

Basically it means that electron-rebuild is rebuilding, but for the wrong version. I think it's a wrong resolution here for new electron versions: https://github.com/electron/electron-rebuild/blob/master/src/rebuild.ts#L124
electron-rebuild uses node-abi 3.0.0, whereas node-abi is at 3.2.0, so adding the following to my package.json did the trick:

"resolutions": { "**/node-abi": "^3.2.0" },

Since node-abi is basically only used as a reference table, I'm fine with "overwriting" to the newest version, there are not breaking "feature" changes, it just houses the latest abi numbers for node versions.

I opened an issue at electron/rebuild#886 since it's an issue on their end, I think we can close the issue here.

@kryops
Copy link

kryops commented Oct 5, 2021

@tom2strobl thanks so much for your analysis, I can confirm that the resolutions workaround fixes it for us as well! 😊

@johnpyp
Copy link

johnpyp commented Oct 6, 2021

@tom2strobl Thanks, resolution fix working for us now too, along with a cloned version of better-sqlite3, that has updated GH releases to provide normal, fast remote prebuild-install 😄

@cloverich
Copy link

Linking #694

@JoshuaWise
Copy link
Member

This should be fixed in v7.4.4.

@zkrising
Copy link

zkrising commented Nov 1, 2021

I'm not sure this is fixed in v7.4.4. I'm getting the exact same error:

...was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 98. 

With better-sqlite at v7.4.4, repeatedly ran yarn rebuild and other things, can't seem to get it to version 98 at all.

node -v is 16.5, which is 1:1 with electron 15. This should work.

Also related, nowhere on https://nodejs.org/en/download/releases/ does it list a version of NodeJS that has a NODE_MODULE_VERSION of 98. Am I missing something?

@tom2strobl
Copy link

Also related, nowhere on https://nodejs.org/en/download/releases/ does it list a version of NodeJS that has a NODE_MODULE_VERSION of 98. Am I missing something?

See my comment above, 98 is an abi version, which you can find in this list: https://github.com/nodejs/node/blob/master/doc/abi_version_registry.json

@zkrising
Copy link

zkrising commented Nov 2, 2021

See my comment above, 98 is an abi version, which you can find in this list: nodejs/node@master/doc/abi_version_registry.json

Ah, that's confusing. I managed to get this to sorta build by running electron-rebuild, but interestingly things like npm rebuild and yarn rebuild seem to change the ABI version to 89? Very weird.

@johnpyp
Copy link

johnpyp commented Nov 2, 2021

@zkldi If you're using electron-builder, I'd recommend using a postinstall script to run electron-builder install-app-deps. It's generally more reliable than electron-rebuild in my experience. E.g:

"scripts": {
    "postinstall": "electron-builder install-app-deps"
}

@Picono435
Copy link

This issue is also happening to me still in 7.4.4? What did I do wrong?

@electrovir
Copy link

I don't get it, how are we supposed to fix this? I've added

"scripts": {
    "postinstall": "electron-builder install-app-deps"
},
"resolutions": { "**/node-abi": "^3.2.0" },

to my package.json file and tried dozens of other combinations of fixes from StackOverflow and GitHub issues but no matter what I do I get:

was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 99. Please try re-compiling or re-installing
the module

@Lermatroid
Copy link

@electrovir did you find a solution to this?

@electrovir
Copy link

@electrovir did you find a solution to this?

@Lermatroid Nope 😕
My workaround so far is to just use JSON files instead 😆

@jsaraiva
Copy link

@electrovir did you find a solution to this?

Hello. Although I haven't had this problem in recent releases, the "buildDependenciesFromSource" option always worked for me.
#713 (comment)
Maybe it helps.

Cheers.

@greg9504
Copy link

greg9504 commented Feb 15, 2022

I have "postinstall": "electron-builder install-app-deps" set and I added the
"build": {
"buildDependenciesFromSource": true
}
but neither worked when trying to use 7.4.0/7.4.4/7.5.0 of better-sqlite3.

I kept getting "was compiled against a different Node.js version using" error.

Solution below
@electrovir @Lermatroid

If you are getting this error:

was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 99. Please try re-compiling or re-installing
the module

I believe the wrong node headers are being downloaded. As an example I'm trying to build for Electron 16, so need NODE_MODULE_VERSION 99. However instead the standard headers (not the Electron ones) for Node version 16..0..0 are being downloaded (NODE_MODULE_VERSION 93) when I run the following command for better-sqlite3

build-release --target=16.0.0 --arch=x64 --dist-url=https://electronjs.org/headers

after which you will see that the wrong node distribution is being downloaded, look carefully at the output here and you will see that it is retrieved from:
gyp http GET https://nodejs.org/dist/v16.0.0/node-v16.0.0-headers.tar.gz
It should be coming from electronjs.org

C:\src\opensourcejs\angular13-electron16\node_modules\better-sqlite3>npm run build-release --target=16.0.0 --arch=x64 --dist-url=https://electronjs.org/headers

> [email protected] build-release
> node-gyp rebuild --release

gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info find Python using Python version 3.7.8 found at "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe"
gyp http GET https://nodejs.org/dist/v16.0.0/node-v16.0.0-headers.tar.gz       <---- NOT DL from electronjs.org
gyp http 200 https://nodejs.org/dist/v16.0.0/node-v16.0.0-headers.tar.gz
gyp http GET https://nodejs.org/dist/v16.0.0/SHASUMS256.txt
gyp http GET https://nodejs.org/dist/v16.0.0/win-x86/node.lib
gyp http GET https://nodejs.org/dist/v16.0.0/win-arm64/node.lib
gyp http GET https://nodejs.org/dist/v16.0.0/win-x64/node.lib
gyp http 200 https://nodejs.org/dist/v16.0.0/SHASUMS256.txt
gyp http 200 https://nodejs.org/dist/v16.0.0/win-x64/node.lib
gyp http 200 https://nodejs.org/dist/v16.0.0/win-x86/node.lib
gyp http 404 https://nodejs.org/dist/v16.0.0/win-arm64/node.lib
gyp info find VS using VS2015 (14.0) found at:
gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio 14.0"
gyp info find VS run with --verbose for detailed information
(rest of build output deleted)

And you will see if you go to your node-gyp cache
C:\Users\YOURUSERNAME\AppData\Local\node-gyp\Cache\16.0.0\include\node\node_version.h
that NODE_MODULE_VERSION is set to 93.

This is the node that better-sqliite3 was built against, and it's wrong.

Now delete this directory, in my case:
C:\Users\YOURUSERNAME\AppData\Local\node-gyp\Cache\16.0.0

To get the correct Electron "node" I used this build command:
node-gyp rebuild --target=16.0.0 --arch=x64 --dist-url=https://electronjs.org/headers

C:\src\opensourcejs\angular13-electron16\node_modules\better-sqlite3>node-gyp rebuild --target=16.0.0 --arch=x64 --dist-url=https://electronjs.org/headers
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info find Python using Python version 2.7.16 found at "C:\Python27\python.exe"
gyp http GET https://electronjs.org/headers/v16.0.0/node-v16.0.0-headers.tar.gz  <-- Correct headers from electronjs.org
gyp http 200 https://electronjs.org/headers/v16.0.0/node-v16.0.0-headers.tar.gz
gyp http GET https://electronjs.org/headers/v16.0.0/SHASUMS256.txt
gyp http GET https://electronjs.org/headers/v16.0.0/win-x86/node.lib
gyp http GET https://electronjs.org/headers/v16.0.0/win-x64/node.lib
gyp http GET https://electronjs.org/headers/v16.0.0/win-arm64/node.lib
gyp http 200 https://electronjs.org/headers/v16.0.0/win-arm64/node.lib
gyp http 200 https://electronjs.org/headers/v16.0.0/SHASUMS256.txt
gyp http 200 https://electronjs.org/headers/v16.0.0/win-x86/node.lib
gyp http 200 https://electronjs.org/headers/v16.0.0/win-x64/node.lib
gyp info find VS using VS2019 (16.9.31229.75) found at:
gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional"
(rest of build output deleted)

I have no idea why the first build script failed yet the second worked (taken from here https://www.electronjs.org/docs/latest/tutorial/using-native-node-modules)

When looking at the better-sqlite3 package.json the command
npm run build-release --target=16.0.0 --arch=x64 --dist-url=https://electronjs.org/headers
should use the correct electron node, as the the target and arch seem to get passed on to node-gyp but not the --dist-url

And I'm not sure why the electron builder "postinstall": "electron-builder install-app-deps" isn't doing the right thing.

Anyway if you got this far at least you'll understand WHERE the NODE_MODULE_VERSION is set to 93 and WHY and how to manually fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests