Skip to content

Sharp w/ Electron not loading (electron-forge) #1951

Closed

Description

@andrewrt

Problem:
I'm trying to use sharp w/ electron.
Everything (sharp+electron) is working in OSX.
On Windows (using gitbash), I'm able to use sharp globally installed with a simple node.js test, but not in electron.
As soon as I uncomment
const sharp = require('sharp');
the app white-screens and doesn't load (no further errors / hints / direction).

Install
Sharp installs w/o errors:
verbose output from fresh install: link
non-verbose:

$ npm i --save sharp

[email protected] install C:\Users\andrew\code\js\electron\spec-electron\node_modules\sharp
(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.8.1/libvips-8.8.1-win32-x64.tar.gz
info sharp Creating C:\Users\andrew\code\js\electron\spec-electron\node_modules\sharp\build\Release
info sharp Copying DLLs from C:\Users\andrew\code\js\electron\spec-electron\node_modules\sharp\vendor\lib to C:\Users\andrew\code\js\electron\spec-electron\node_modules\sharp\build\Release

  • [email protected]
    added 13 packages from 65 contributors and audited 14365 packages in 21.978s
    found 20 vulnerabilities (5 low, 8 moderate, 6 high, 1 critical)
    run npm audit fix to fix them, or npm audit for details

Environment:

$ npx envinfo --binaries --languages --system -- utilities
npx: installed 1 in 1.828s

System:
OS: Windows 10
CPU: (2) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Memory: 3.40 GB / 7.10 GB
Binaries:
Node: 12.4.0 - C:\Program Files\nodejs\node.EXE
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
Languages:
Bash: 4.4.23 - C:\Program Files\Git\usr\bin\bash.EXE
Perl: 5.26.2 - C:\Program Files\Git\usr\bin\perl.EXE
Python: 2.7.17 - C:\Python27\python.EXE

Similar question:

This is very similar to #1846, but the suggested solutions aren't working:

Attempting solution from @lovell :

andrew@ANDREWT0C72 MINGW64 ~/code/js/electron/spec-electron (windows10)
$ ./node_modules/.bin/electron-rebuild

  • Searching dependency tree
    √ Rebuild Complete

but problem persists

Attempting solution from Mubaidr:

$ ./node_modules/.bin/electron-builder install-app-deps
• electron-builder version=22.1.0
• rebuilding native dependencies dependencies=@serialport/[email protected], [email protected] platform=win32 arch=x64
• install prebuilt binary name=@serialport/bindings version=8.0.4 platform=win32 arch=x64
• build native dependency from sources name=@serialport/bindings
version=8.0.4
platform=win32
arch=x64
reason=prebuild-install failed with error (run with env DEBUG=electron-builder to get more information)
error=prebuild-install info begin Prebuild-install version 5.3.3
prebuild-install WARN install prebuilt binaries enforced with --force!
prebuild-install WARN install prebuilt binaries may be out of date!
prebuild-install info looking for cached prebuild @ C:\Users\andrew\AppData\Roaming\npm-cache_prebuilds\afca11-bindings-v8.0.4-electron-v73-win32-x64.tar.gz
prebuild-install http request GET https://github.com/serialport/node-serialport/releases/download/v8.0.4/bindings-v8.0.4-electron-v73-win32-x64.tar.gz
prebuild-install http 404 https://github.com/serialport/node-serialport/releases/download/v8.0.4/bindings-v8.0.4-electron-v73-win32-x64.tar.gz
prebuild-install WARN install No prebuilt binaries found (target=6.1.4 runtime=electron arch=x64 libc= platform=win32)

• install prebuilt binary name=sharp version=0.23.2 platform=win32 arch=x64

despite error on prebuild install for serialport - serial port still ends up working, uncommenting sharp still causes the app to fail to load.

I'm getting no errors out in the main console, nor in chrome tools console - so I'm grabbing at straws / looking for any ideas on what else I can try.

Activity

  1. andrewrt commented on Nov 6, 2019

    @andrewrt
    Author

    Steps to reproduce:

    Install electron forge:
    npm install -g [email protected]

    Create electron app & run:

    andrew@ANDREWT0C72 MINGW64 ~/code/js/electron
    $ electron-forge init test-sharp --template=react
    
    andrew@ANDREWT0C72 MINGW64 ~/code/js/electron
    $ cd test-sharp/
    
    andrew@ANDREWT0C72 MINGW64 ~/code/js/electron/test-sharp (master)
    $ electron-forge start

    (see electron working - app says "Welcome to React!")
    Install sharp:

    andrew@ANDREWT0C72 MINGW64 ~/code/js/electron/test-sharp (master)
    $ npm i --save sharp
    
    > [email protected] install C:\Users\andrew\code\js\electron\test-sharp\node_modules\sharp
    > (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
    
    info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.8.1/libvips-8.8.1-win32-x64.tar.gz
    info sharp Creating C:\Users\andrew\code\js\electron\test-sharp\node_modules\sharp\build\Release
    info sharp Copying DLLs from C:\Users\andrew\code\js\electron\test-sharp\node_modules\sharp\vendor\lib to C:\Users\andrew\code\js\electron\test-sharp\node_modules\sharp\build\Release
    npm WARN [email protected] No repository field.
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\electron-installer-flatpak):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin,linux","arch":"any"} (current: {"os":"win32","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\electron-installer-debian):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin,linux","arch":"any"} (current: {"os":"win32","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\macos-alias):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\electron-installer-redhat):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin,linux","arch":"any"} (current: {"os":"win32","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\appdmg):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fs-xattr):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"!win32","arch":"any"} (current: {"os":"win32","arch":"x64"})
    
    + [email protected]
    added 28 packages from 88 contributors and audited 10505 packages in 24.831s
    found 20 vulnerabilities (5 low, 8 moderate, 6 high, 1 critical)
      run `npm audit fix` to fix them, or `npm audit` for details
    

    Open src/app.jsx and add this after the 1st import:
    const sharp = require('sharp');

    run:

    andrew@ANDREWT0C72 MINGW64 ~/code/js/electron/test-sharp (master)
    $ electron-forge start

    Now the app won't launch

  2. lovell commented on Nov 6, 2019

    @lovell
    Owner

    Did you see #1810, which suggests electron-forge v6 is required? The use of electron-rebuild will definitely be required but I think electron-forge runs this for you.

  3. andrewrt commented on Nov 6, 2019

    @andrewrt
    Author

    thanks @lovell - will give it a shot

  4. andrewrt commented on Nov 7, 2019

    @andrewrt
    Author

    Created a new electron-forge v6 project - added sharp, and run - crashes when adding require('sharp'); in app.js.
    Project here: https://github.com/andrewrt/ef6-sharp

    Steps & Tests:

    1. Install electron-forge v6 npm i -g @electron-forge/cli
    2. create electron-forge project w/ webpack electron-forge init ef6-sharp --template=webpack
    3. add react: npm i --save react react-dom
    4. add babel: npm i --save-dev babel-loader @babel/core @babel/preset-env
    5. add .babelrc and update webpack.rules.js & app.js as shown in above repo
    6. run (works).
    7. install sharp & run (still works)
    8. add const sharp = require('sharp'); (fails)
      Output:
    
    An unhandled rejection has occurred inside Forge:
    [Error: EISDIR: illegal operation on a directory, read] {
      errno: -4068,
      code: 'EISDIR',
      syscall: 'read'
    }
    
    Electron Forge was terminated. Location:
    {}
    
    1. try electron-rebuild and run again (still fails)
    2. try build via node-gyp (still fails):
    $ HOME=~/.electron-gyp node-gyp rebuild --target=7.1.0 --arch=x64 --dist-url=https://electronjs.org/headers
    1. Downgrade electron from 7.1.0 to 6.x to line up with Couldn't run in Electron project with electron-forge #1810 & re-run build via electron-rebuild (still fails)
    2. try rebuilding again w/ node-gyp: output link (still fails)
  5. andrewrt commented on Nov 7, 2019

    @andrewrt
    Author

    tried npm-check as well - showing no issues aside from suggesting to go back to the latest electron (7.1.0)

  6. lovell commented on Nov 7, 2019

    @lovell
    Owner
  7. andrewrt commented on Nov 8, 2019

    @andrewrt
    Author

    thanks @lovell - did that - didn't get much more - so as per the reference - seeing if the electron-forge crew might have some insight into how to get more info...

  8. andrewrt commented on Nov 8, 2019

    @andrewrt
    Author

    Got it working @lovell -
    @ShadeOfGod provided the answer here: WiseLibs/better-sqlite3#126

    by adding

    // webpack config
    {
      // ...
      externals: {
        'better-sqlite3': 'commonjs better-sqlite3',
      },
    }
    • now sharp is usable again on windows - and still works on osx as well.

    I've updated the sharp test repo to see it "in action" as well

  9. andrewrt commented on Nov 12, 2019

    @andrewrt
    Author

    I'm going to re-open this one.
    So the above workaround fixes the development environment for sharp+ef6, but the packaged product then leaves out sharp all together.

    I couldn't figure out how to properly pack sharp back into the webpack environment as it's packaged by electron-packager.

    I've tried updating the webpack rules to add in

     plugins: [
        new CopyPlugin([
          {
            from: './node_modules/sharp/',
            to: 'main_window/node_modules/sharp/', // still under node_modules directory so it could find this module
            // ignore: [...]
          },
        ]),
      ]

    but that led me to having to copy over all node_modules as i didn't know the correct path used by electron-forge + webpack.

    Since other native plugins do seem to be working, I'm hoping this is something debuggable by sharp, but it's a bit beyond me to be able to provide much more detail...

  10. emmekappa commented on Aug 21, 2020

    @emmekappa

    @andrewrt any update on this? I'm facing the same issue

  11. Cosmin-Parvulescu commented on Mar 4, 2021

    @Cosmin-Parvulescu

    Try adding sharp as an external in the webpack configuration for your electron main module.

    externals: {
      // ...,
      sharp: 'sharp'
    }
    

    In case this doesn't work by itself, using the electron-rebuild NPM package and doing a manual run of ./node_modules/.bin/electron-rebuild might also help.

  12. aperkaz commented on Apr 11, 2021

    @aperkaz

    Building on what @andrewrt proposed, I managed to make it work in development and on packaged app, on the renderer process.

    Workin example here: https://github.com/aperkaz/electron-forge-sharp

  13. patricktree commented on Dec 25, 2021

    @patricktree

    I was not able to get sharp automatically packaged with the electron application (Windows x64, electron-forge 6.0.0-beta.61).
    It would always show an error dialog when starting the packaged application.

    However, I managed to get it to work - in development mode and the packaged windows app, in the main process - by doing the following:

    1. Add the following to webpack.main.config.js:
    module.exports = {
      // ...
      externals: {
        sharp: 'commonjs sharp',
      }
    }
    1. Copy sharp and it's dependencies manually into a node_modules folder in the .webpack/main folder.

    See this commit for the necessary changes: patricktree/file-explorer@e2bf09c.
    When using a "pure" electron-forge setup, you might need to install copy-webpack-plugin.

    It feels hacky to copy the node modules manually, but at least it works!

  14. Cosmin-Parvulescu commented on Jan 3, 2022

    @Cosmin-Parvulescu

    @pkerschbaum did this end up working for you all the way to production? I remember trying this and it wouldn't ultimately work on Android.

  15. patricktree commented on Jan 3, 2022

    @patricktree

    @Cosmin-Parvulescu This electron-forge project I am working on is only meant to run on Windows and Linux (and Mac OS eventually).
    So far, I tested the packaged application on both Windows x64 and WSL 2 Ubuntu (WSLg), and I can confirm that my approach - copying sharp and its dependencies manually - does work on these platforms.

    But I don't know if it would work on Android.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions