Description
- Electron-Builder Version:
25.0.5
- Node Version:
22.8.0
- npm Version:
10.8.0
- Electron Version:
32.1.0
- Electron Type (current, beta, nightly):
current
- Target:
macOS arm64
There's an issue with the latest electron-builder
where after packaging an app, the node_modules
directory will include wrong module versions for sub-modules.
I'll explain better with a reproducible example:
- Run this command:
npm create --yes node-llama-cpp@3.0.0-beta.46 -- --template electron-typescript-react --name electron-builder-issue
This scaffolds a
node-llama-cpp
Electron project that allows you to run LLMs locally on your machine - Select any model from the list, it doesn't matter which one for this issue reproduction
cd electron-builder-issue
- Open
package.json
and remove thepostinstall
script to skip the model download npm install
npm run build
- Launch the app you built (under the
release/mac-arm64
directory) and see that it works - Close the app
- Open
package.json
. UnderdevDependencies
, change theelectron-builder
version to25.0.5
npm install
npm run build
- Launch the app and see that it fails to open with some error.
Sometimes it's aboutrequire() of ES Module
, sometimes it's about a missing import, it's pretty random from my testings.
Locate from the long error which module is "the importer" (the one trying to import another module), and which module is "the imported" (the module that was attempted to be imported).
This is the error I received when trying to open the app:
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user/Documents/workspace/electron-builder-issue/release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/app.asar/node_modules/ansi-regex/index.js from /Users/user/Documents/workspace/electron-builder-issue/release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/app.asar/node_modules/string-width/node_modules/strip-ansi/index.js not supported.
Instead change the require of /Users/user/Documents/workspace/electron-builder-issue/release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/app.asar/node_modules/ansi-regex/index.js in /Users/user/Documents/workspace/electron-builder-issue/release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/app.asar/node_modules/string-width/node_modules/strip-ansi/index.js to a dynamic import() which is available in all CommonJS modules.
at c._load (node:electron/js2c/node_init:2:17025)
at Object.<anonymous> (/Users/user/Documents/workspace/electron-builder-issue/release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/app.asar/node_modules/string-width/node_modules/strip-ansi/index.js:2:19)
In my error:
"The importer" is: string-width/node_modules/strip-ansi
"The imported" is: ansi-regex
The above error happened because some of the code was trying to load some version of a module but instead received a completely different version of it.
Here's how you can validate that this is indeed the issue:
cd release/mac-arm64/node-llama-cpp-electron-example.app/Contents/Resources/
npx asar e app.asar app.content
cat ./app.content/node_modules/ansi-regex/package.json
and see that the version of the bundledansi-regex
is6.1.0
cat ./app.content/node_modules/string-width/node_modules/strip-ansi/package.json
and see this in thepackage.json
ofstrip-ansi
:"dependencies": { "ansi-regex": "^5.0.1" }
As you can see, strip-ansi
expected a ^5.0.1
version of ansi-regex
, but actually received 6.1.0
, which is incompatible.
From my testings, this issue doesn't happen with electron-builder
version 25.0.0
, but does happen with 25.0.1
.
Because of this issue, I downgraded the version of electron-builder
in the node-llama-cpp
Electron template for now until this is resolved.