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

Cannot run a packaged electron forge app #4116

Closed
5 of 11 tasks
neocrao opened this issue May 28, 2024 · 10 comments
Closed
5 of 11 tasks

Cannot run a packaged electron forge app #4116

neocrao opened this issue May 28, 2024 · 10 comments

Comments

@neocrao
Copy link

neocrao commented May 28, 2024

Possible install-time or require-time problem

You must confirm both of these before continuing.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest,
please open an issue against that package instead.

Are you using a supported runtime?

  • I am using Node.js with a version that satisfies ^18.17.0 || ^20.3.0 || >=21.0.0
  • I am using Deno
  • I am using Bun

If you cannot confirm any of these,
please upgrade to the latest version
and try again before opening an issue.

Are you using a supported package manager and installing optional dependencies?

  • I am using npm >= 9.6.5 with --include=optional
  • I am using yarn >= 3.2.0
  • I am using pnpm >= 7.1.0 with --no-optional=false
  • I am using Deno
  • I am using Bun

If you cannot confirm any of these, please upgrade to the latest version of your chosen package manager
and ensure you are allowing the installation of optional or multi-platform dependencies before opening an issue.

What is the complete error message, including the full stack trace?

Below are some of the things I have tried as I scoured through the the reported issues in this repository. None of the solutions worked for me. I am hoping that its something simple that I have missed.

I'd appreciate any help.

Steps to reproduce with different attempts to solve the issue:

  1. Setup an electron forge project (neocrao/electron-forge-sharp@cb18b5d)

    npm init electron-app@latest my-app -- --template=webpack-typescript
    

    npm run start and npm run make work without any issues

  2. Install sharp (neocrao/electron-forge-sharp@f6571bd)

    npm install -S --include=optional sharp
    
  3. Use sharp in the project (neocrao/electron-forge-sharp@fae3202)

    npm run start works.

    But packaging an app using npm run make, and then running the packaged app, gives the following error.

    A JavaScript error occurred in the main process
    
    Uncaught Exception:
    Error: Could not load the "sharp" module using the darwin-x64 runtime
    Possible solutions:
    - Ensure optional dependencies can be installed:
    npm install --include=optional sharp
    yarn add sharp --ignore-engines
    - Ensure your package manager supports multi-platform installation:
    See https://sharp.pixelplumbing.com/install#cross-platform
    - Add platform-specific dependencies:
    npm install --os=darwin --cpu=x64 sharp
    - Consult the installation documentation:
    See https://sharp.pixelplumbing.com/install
    at 5682 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:108667)
    at r (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117126)
    at 1842 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:55203)
    at r (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117126)
    at 7442 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:58829)
    at r (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117126)
    at 5391 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:49603)
    at r (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117126)
    at /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117220
    at Object.<anonymous> (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:117246)
    

  4. Added 'sharp': 'commonjs sharp', to webpack (neocrao/electron-forge-sharp@ae0088e)

    Packaged app with npm run make throws error:

    A JavaScript error occurred in the main process
    
    Uncaught Exception:
    Error: Cannot find module 'sharp'
    Require stack:
    - /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js
    - 
    at Module._resolveFilename (node:internal/modules/cjs/loader:1151:15)
    at s._resolveFilename (node:electron/js2c/browser_init:2:120292)
    at Module._load (node:internal/modules/cjs/loader:992:27)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Module.require (node:internal/modules/cjs/loader:1242:19)
    at require (node:internal/modules/helpers:176:18)
    at 288 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7925)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    at 391 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7367)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    

  5. Added asar unpack paths (neocrao/electron-forge-sharp@1894d02)

    Packaged app with npm run make throws error:

    A JavaScript error occurred in the main process
    
    Uncaught Exception:
    Error: Cannot find module 'sharp'
    Require stack:
    - /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js
    - 
    at Module._resolveFilename (node:internal/modules/cjs/loader:1151:15)
    at s._resolveFilename (node:electron/js2c/browser_init:2:120292)
    at Module._load (node:internal/modules/cjs/loader:992:27)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Module.require (node:internal/modules/cjs/loader:1242:19)
    at require (node:internal/modules/helpers:176:18)
    at 288 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7925)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    at 391 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7367)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    

  6. Tried @timfish/forge-externals-plugin (neocrao/electron-forge-sharp@311d5e6)

    Packaged app with npm run make throws error:

    A JavaScript error occurred in the main process
    
    Uncaught Exception:
    Error: Cannot find module 'sharp'
    Require stack:
    - /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js
    - 
    at Module._resolveFilename (node:internal/modules/cjs/loader:1151:15)
    at s._resolveFilename (node:electron/js2c/browser_init:2:120292)
    at Module._load (node:internal/modules/cjs/loader:992:27)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Module.require (node:internal/modules/cjs/loader:1242:19)
    at require (node:internal/modules/helpers:176:18)
    at 288 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7925)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    at 391 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7367)
    at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8384)
    

  7. Tried copy-webpack-plugin (neocrao/electron-forge-sharp@bce90da)

    Packaged app with npm run make throws error:

    A JavaScript error occurred in the main process
    
    Uncaught Exception:
    Error: Could not load the "sharp" module using the darwin-x64 runtime
    ERR_DLOPEN_FAILED: dlopen(/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/sharp-darwin-x64.node, 0x0001): Library not loaded: @rpath/libvips-cpp.42.dylib
    Referenced from: <D0807ED6-A466-3720-9FAA-283E57494039> /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/sharp-darwin-x64.node
    Reason: tried: '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../sharp-libvips-darwin-x64/1.0.2/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../../../@img-sharp-libvips-darwin-x64-npm-1.0.2-01b146c37d/node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../sharp-libvips-darwin-x64/1.0.2/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar.unpacked/node_modules/@img/sharp-darwin-x64/lib/../../../../../@img-sharp-libvips-darwin-x64-npm-1.0.2-01b146c37d/node_modules/@img/sharp-libvips-darwin-x64/lib/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Libraries/libvips-cpp.42.dylib' (no such file), '/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Frameworks/libvips-cpp.42.dylib' (no such file), '/usr/local/lib/libvips-cpp.42.dylib' (no such file), '/usr/lib/libvips-cpp.42.dylib' (no such file, not in dyld cache)
    Possible solutions:
    - Ensure optional dependencies can be installed:
    npm install --include=optional sharp
    yarn add sharp --ignore-engines
    - Ensure your package manager supports multi-platform installation:
    See https://sharp.pixelplumbing.com/install#cross-platform
    - Add platform-specific dependencies:
    npm install --os=darwin --cpu=x64 sharp
    - Consult the installation documentation:
    See https://sharp.pixelplumbing.com/install
    at Object.<anonymous> (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/node_modules/sharp/lib/sharp.js:114:9)
    at Module._compile (node:internal/modules/cjs/loader:1391:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1451:10)
    at Module.load (node:internal/modules/cjs/loader:1214:32)
    at Module._load (node:internal/modules/cjs/loader:1030:12)
    at c._load (node:electron/js2c/node_init:2:13672)
    at Module.require (node:internal/modules/cjs/loader:1242:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/node_modules/sharp/lib/constructor.js:10:1)
    

What is the complete output of running npm install --verbose --foreground-scripts sharp in an empty directory?

npm verb cli /Users/neo/.nvm/versions/node/v20.11.1/bin/node /Users/neo/.nvm/versions/node/v20.11.1/bin/npm
npm info using npm@10.2.4
npm info using node@v20.11.1
npm verb title npm install sharp
npm verb argv "install" "--loglevel" "verbose" "--foreground-scripts" "sharp"
npm verb logfile logs-max:10 dir:/Users/neo/.npm/_logs/2024-05-28T01_41_48_844Z-
npm verb logfile /Users/neo/.npm/_logs/2024-05-28T01_41_48_844Z-debug-0.log
npm http fetch GET 200 https://registry.npmjs.org/sharp 191ms (cache revalidated)
npm http fetch GET 200 https://registry.npmjs.org/color 59ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linux-arm 55ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-darwin-arm64 57ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linux-x64 56ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linux-arm64 58ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linuxmusl-arm64 58ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/semver 64ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linux-arm 62ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linux-s390x 63ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linux-arm64 65ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-win32-ia32 64ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linux-x64 66ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-darwin-x64 73ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linuxmusl-x64 70ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-wasm32 71ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linux-s390x 76ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-darwin-x64 80ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-linuxmusl-arm64 76ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/detect-libc 85ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-darwin-arm64 87ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-libvips-linuxmusl-x64 85ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@img%2fsharp-win32-x64 84ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/color-string 20ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/@emnapi%2fruntime 31ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/color-convert 28ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/tslib 60ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/color-name 16ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/color-name 17ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/simple-swizzle 19ms (cache hit)
npm http fetch GET 200 https://registry.npmjs.org/is-arrayish 7ms (cache hit)
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-win32-x64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-win32-ia32
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-wasm32
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@emnapi/runtime
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/tslib
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linuxmusl-x64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linuxmusl-arm64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linux-x64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linux-s390x
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linux-arm64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-linux-arm
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linuxmusl-x64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linuxmusl-arm64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linux-x64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linux-s390x
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linux-arm64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-linux-arm
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-libvips-darwin-arm64
npm verb reify failed optional dependency /Users/neo/Projects/publicInfo/test/node_modules/@img/sharp-darwin-arm64
npm info run sharp@0.33.4 install node_modules/sharp node install/check

> sharp@0.33.4 install
> node install/check

npm info run sharp@0.33.4 install { code: 0, signal: null }

added 11 packages in 4s

3 packages are looking for funding
  run `npm fund` for details
npm verb exit 0
npm info ok

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

  System:
    OS: macOS 13.4
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 53.11 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.11.1/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.15.5 - /usr/local/bin/pnpm
@lovell
Copy link
Owner

lovell commented May 28, 2024

Looking at https://github.com/neocrao/electron-forge-sharp

@neocrao
Copy link
Author

neocrao commented May 29, 2024

Hello @lovell! Thank you for the suggestion.

Unfortunately, it doesn't seem to work.

Commit: neocrao/electron-forge-sharp@08b6cb5

Overall changes since sharp was added: neocrao/electron-forge-sharp@fae3202...08b6cb5

A JavaScript error occurred in the main process

Uncaught Exception:
Error: Cannot find module 'sharp'
Require stack:
- /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js
- 
at Module._resolveFilename (node:internal/modules/cjs/loader:1151:15)
at s._resolveFilename (node:electron/js2c/browser_init:2:120292)
at Module._load (node:internal/modules/cjs/loader:992:27)
at c._load (node:electron/js2c/node_init:2:13672)
at Module.require (node:internal/modules/cjs/loader:1242:19)
at require (node:internal/modules/helpers:176:18)
at 907 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7927)
at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8386)
at 288 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7369)
at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8386)

@lovell
Copy link
Owner

lovell commented May 29, 2024

remove all other webpack plugins that attempt to fix this

Looks like @vercel/webpack-asset-relocator-loader is still in there, please remove.

@neocrao
Copy link
Author

neocrao commented May 29, 2024

@vercel/webpack-asset-relocator-loader (as well as node-loader) came with the electron forge project by default. They weren't added explicitly by me. Reference

Update: neocrao/electron-forge-sharp@67c6331

Still fails with similar error:

A JavaScript error occurred in the main process

Uncaught Exception:
Error: Cannot find module 'sharp'
Require stack:
- /Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js
- 
at Module._resolveFilename (node:internal/modules/cjs/loader:1151:15)
at s._resolveFilename (node:electron/js2c/browser_init:2:120292)
at Module._load (node:internal/modules/cjs/loader:992:27)
at c._load (node:electron/js2c/node_init:2:13672)
at Module.require (node:internal/modules/cjs/loader:1242:19)
at require (node:internal/modules/helpers:176:18)
at 288 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7927)
at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8388)
at 229 (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:7369)
at t (/Users/neo/Projects/publicInfo/my-app/out/my-app-darwin-x64/my-app.app/Contents/Resources/app.asar/.webpack/main/index.js:1:8388)

I also performed the following to make sure it was in a clean state, but ended with the same error as above.

  1. Deleted node_modules, package-lock.json
  2. Removed sharp from package.json
  3. npm install
  4. npm install -S --include=optional sharp

@lovell
Copy link
Owner

lovell commented May 29, 2024

Ah, I remember now, this is due to electron/forge#1250 and came up previously in #3640 (comment)

You'll probably want to use @timfish/forge-externals-plugin until electron-forge fixes this.

@neocrao
Copy link
Author

neocrao commented May 30, 2024

I did try this @timfish/forge-externals-plugin (neocrao/electron-forge-sharp@311d5e6)

But while working with you, I started questioning the glob pattern that I put in for asar unpack, and it turned out that it was the issue.
I fixed the glob pattern as follows, and then everything started to work! The Electron Forge package doesnt take in an array of paths, so it needs to be a pattern from what I understand.

- unpack: "(**/node_modules/sharp/**/*|**/node_modules/@img/**/*)"
+ unpack: "**/node_modules/{sharp,@img}/**/*"

As a summary, below are the details on whats required to make sharp work with Electron Forge generated package with webpack + typescript. Hopefully this can help others.

Setup steps:

  1. Setup Electron Forge project with Webpack and Typescript:

    npm init electron-app@latest my-app -- --template=webpack-typescript
    cd my-app
    npm install
    
  2. Install sharp

    npm install -S --include=optional sharp
    
  3. Install @timfish/forge-externals-plugin

    npm i -D @timfish/forge-externals-plugin
    
  4. Make the following changes to forge.config.ts

    const config: ForgeConfig = {
      packagerConfig: {
    -   asar: true,
    +   asar: {
    +     unpack: "**/node_modules/{sharp,@img}/**/*"
    +   }
      },
      rebuildConfig: {},
      makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
      plugins: [
        new AutoUnpackNativesPlugin({}),
        new WebpackPlugin({
          ...
          ...
        }),
    +   // This needs to be after WebpackPlugin
    +   {
    +     name: "@timfish/forge-externals-plugin",
    +     config: {
    +       "externals": ["sharp"],
    +       "includeDeps": true
    +     }
    +   },
        ...
        ...
      ],
    };
  5. Create packaged app using npm run make, or run in dev mode using npm run start

Thank you so much for the help! Please feel free to close this issue.

@lovell lovell closed this as completed May 30, 2024
@Jaarson
Copy link

Jaarson commented Sep 6, 2024

Isn't this approaching including prebuilds for ALL platforms into the final package?

I'm looking for a way to include only the prebuilt files for the specific target platform, so that the package is not getting too big unnecessarily.

@Ymihere03
Copy link

This solution fixed a very similar problem I was having in Windows.
sharp-win32-x64.node could not be found

ERR_DLOPEN_FAILED: The specified module could not be found.
\\?\C:\...\resources\app.asar.unpacked\node_modules\@img\sharp-win32-x64\lib\sharp-win32-x64.node

The step for updating forge.config.ts solved my issue.

packagerConfig: {
-   asar: true,
-  "asarUnpack": [
-      "**/node_modules/sharp/**/*",
-      "**/node_modules/@img/**/*"
-    ]
+   asar: {
+    unpack: "**/node_modules/{sharp,@img}/**/*"
   }
 },

Ymihere03 added a commit to Ymihere03/sharp that referenced this issue Nov 11, 2024
Update install documentation for Electron

Renamed the previous Electron section to 'electron-builder' and fixed the broken external link

Added a new section for 'electron-forge' with its own external link to electron-forge documentation and included sharp ASAR configuration. The config for 'electron-forge' is based on part of the resolution in lovell#4116
@rdyar
Copy link

rdyar commented Nov 22, 2024

I can't believe I just spent 3 hours trying to get this to work cause I didn't notice the difference between:

asar: true,
asarunpacked: "**/node_modules/{sharp,@img}/**/*"

and

 asar: { unpack: "**/node_modules/{sharp,@img}/**/*" },

Also it wasn't clear above if you needed @timfish/forge-externals-plugin but for me I did - I tested after I saw my mistake and without it I get an error it can't find sharp.

my forge.config.js file:

const { FusesPlugin } = require("@electron-forge/plugin-fuses");
const { FuseV1Options, FuseVersion } = require("@electron/fuses");
const path = require("path");

module.exports = {
  packagerConfig: {
    asar: { unpack: "**/node_modules/{sharp,@img}/**/*" },
    afterCopy: [
      (buildPath, electronVersion, platform, arch, callback) => {
        console.log("Build path:", buildPath);
        console.log("asarUnpack paths exist:", {
          sharp: require("fs").existsSync(
            path.join(buildPath, "node_modules/sharp")
          ),
        });
        callback();
      },
    ],
  },
  rebuildConfig: {},
  makers: [
    {
      name: "@electron-forge/maker-squirrel",
      config: {},
    },
    {
      name: "@electron-forge/maker-zip",
      platforms: ["darwin"],
    },
    {
      name: "@electron-forge/maker-deb",
      config: {},
    },
    {
      name: "@electron-forge/maker-rpm",
      config: {},
    },
  ],
  plugins: [
    {
      name: "@electron-forge/plugin-auto-unpack-natives",
      config: {},
    },
    {
      name: "@electron-forge/plugin-webpack",
      config: {
        mainConfig: "./webpack.main.config.js",
        renderer: {
          config: "./webpack.renderer.config.js",
          entryPoints: [
            {
              html: "./src/renderer/index.html",
              js: "./src/renderer/renderer.js",
              name: "main_window",
              preload: {
                js: "./src/preload/index.js",
              },
            },
          ],
        },
      },
    },
    {
      name: "@timfish/forge-externals-plugin",
      config: {
        externals: ["sharp"],
        includeDeps: true,
      },
    },
    new FusesPlugin({
      version: FuseVersion.V1,
      [FuseV1Options.RunAsNode]: false,
      [FuseV1Options.EnableCookieEncryption]: true,
      [FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
      [FuseV1Options.EnableNodeCliInspectArguments]: false,
      [FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
      [FuseV1Options.OnlyLoadAppFromAsar]: true,
    }),
  ],
};

this was a handy way to see if the file existed:

afterCopy: [
      (buildPath, electronVersion, platform, arch, callback) => {
        console.log("Build path:", buildPath);
        console.log("asarUnpack paths exist:", {
          sharp: require("fs").existsSync(
            path.join(buildPath, "node_modules/sharp")
          ),
        });
        callback();
      },
    ],

thanks so much for maintaining this, it is much appreciated.

@Reedo0910
Copy link

@rdyar Thank you, you saved my day! I spent five hours looking for the cause, and your solution really worked for me.

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

No branches or pull requests

6 participants