lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.

Home Page:https://sharp.pixelplumbing.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot run a packaged electron forge app

neocrao opened this issue · comments

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

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

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)

remove all other webpack plugins that attempt to fix this

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

@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

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.

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.