Sharp: Sharp w/ Electron not loading (electron-forge)

Created on 6 Nov 2019  Â·  11Comments  Â·  Source: lovell/sharp

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\sharpbuild\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\sharpbuild\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.

question

Most helpful comment

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

All 11 comments

and for posterity - using node-gyp to install:
https://gist.github.com/andrewrt/fa3a08f7346ef2c023565ed07c1fecd7

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

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.

thanks @lovell - will give it a shot

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 #1810 & re-run build via electron-rebuild (still fails)
  2. try rebuilding again w/ node-gyp: output link (still fails)

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

If you didn't see it, setting the DEBUG environment variable as instructed in https://github.com/electron-userland/electron-forge/blob/master/SUPPORT.md#troubleshooting might help.

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...

Got it working @lovell -
@shadeofgod provided the answer here: https://github.com/JoshuaWise/better-sqlite3/issues/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

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...

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

natural-law picture natural-law  Â·  3Comments

Andresmag picture Andresmag  Â·  3Comments

OleVik picture OleVik  Â·  3Comments

emmtte picture emmtte  Â·  3Comments

genifycom picture genifycom  Â·  3Comments