node-gfx / node-canvas-prebuilt

Repo used to build binaries for node-canvas on CI
170 stars 31 forks source link

Run from electron #30

Closed Apidcloud closed 5 years ago

Apidcloud commented 6 years ago

Good evening!

I wonder if it's possible to use this package from within electron?

I'm using electron 1.7.11 and canvas-prebuilt 1.6.5-prerelease.1, but when I attempt to do so, the following error is raised:

Uncaught Error: A dynamic link library (DLL) initialization routine failed.
\\?\D:\Workspace\GitHub\_Scarlett\scarlett-editor\node_modules\canvas-prebuilt\canvas\build\Release\canvas.node
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20)
    at Object.Module._extensions..node (module.js:598:18)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Workspace\GitHub\_Scarlett\scarlett-editor\node_modules\canvas-prebuilt\canvas\lib\bindings.js:3:18)
    at Object.<anonymous> (D:\Workspace\GitHub\_Scarlett\scarlett-editor\node_modules\canvas-prebuilt\canvas\lib\bindings.js:5:3)

The output yielded from windows.process.version is v7.9.0.

Any ideas on how to build this package targeting electron? I've read something about electron-rebuild. Could it be used somehow?

Thanks!

chearon commented 6 years ago

It could be that Electron loads a DLL that canvas-prebuilt does as well (e.g. libz2-1.dll) and they're conflicting.

The best way to know that is get Dependency Walker and point it at the Electron executable, then see if it or any of its dependencies are the same name as any of the ones in node_modules/canvas-prebuilt/build/Release.

Hard for me to say much more than that as the error doesn't give us much detail and I don't have Windows anymore, but that's my best guess.

Apidcloud commented 6 years ago

From what I've been reading online, I think it might be due to the fact electron (v1.7.11) uses a specific version of node within (7.9), whereas I have 8.9.4 installed. Is there a way to build is specifically for that version instead?

Thanks!

chearon commented 6 years ago

Oh, if you're right that isn't too hard, there's a --target option. Just do npm install --target 7.9 canvas-prebuilt

Apidcloud commented 6 years ago

I'll give that a go once I have the chance then!

Apidcloud commented 6 years ago

Hmm no luck with node 7.9 and npm 4.2:

npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "--target" "7.9" "canvas-prebuilt"
npm ERR! node v7.9.0
npm ERR! npm  v4.2.0

npm ERR! Cannot convert undefined or null to object

Probably because that version of npm doesn't support the flag. Tried with node 8.9.4 and npm 5.4, and got the following error:

npm ERR! No valid versions available for 7.9

I think it has to do with the npm registry, meaning it can't find the package targeting 7.9?

I can install it with no problems without the flag.

chearon commented 6 years ago

Looks like you actually need a = and the 4 digit number:

$ npm install canvas-prebuilt --target=7.9.0
Apidcloud commented 6 years ago

That did it, but still no luck within electron 😢

Can we try this? (the manual rebuild for electron 1.7.11, specifically) https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md

I'll give it a try as well.

Apidcloud commented 6 years ago

Was able to use node-gyp rebuild --target=1.7.11 --arch=x64 --dist-url=https://atom.io/download/electron with no errors! I'll keep going.

Edit: It seems to work!!! 💯 Could you please release a version targeting electron 1.7.11 then? The one I'm using (through msdf-bmfont-xml - see related issue https://github.com/soimy/msdf-bmfont-xml/issues/18) is 1.6.5-prerelease.1

tommypreger commented 6 years ago

I'm also trying to run this from Electron without luck. I'm using the latest alpha (I need registerFont).

This is the error:

Uncaught Error: The module 'node_modules/canvas-prebuilt/canvas/build/Release/canvas.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 57. This version of Node.js requires NODE_MODULE_VERSION 54.

Apidcloud commented 6 years ago

@chearon From my understanding, I'm basically rebuilding node-canvas with a specific electron version in mind, correct? Can you tell how to create a prebuilt version that I can publish and use as a dependency? For now I have been doing it locally, linking projects, but I would like to automate it if possible.

Or does the new version allow me to set some flags in that regard?

Thank you!

tettusud commented 5 years ago

any luck with this issue? I am still facing the issue. I use node v10 , electron 3 and canvas. I get the same DLL issue

das-solo commented 5 years ago

@Apidcloud I have also come across this issue. How did you do it locally by linking projects? I have also tried building it by using node-gyp and electron-rebuild with zero success?

Apidcloud commented 5 years ago

@das-solo I was able to do so by installing node-gyp and using it to rebuild the canvas module within canvas-prebuilt.

That is, open node_modules/canvas-prebuilt/canvas inside the project. Then run node-gyp rebuild with some target and architecture flags (e.g., —target=1.7.11 —arch=x64)

Afterwards, just go to the project’s main folder and run npm or yarn link.

Finally, just go to the other project (that is using the first as a dependency) and run link again (e.g., yarn link “first-project-package-name")

That was the only way I got it to work locally. But I still need to figure out a way of doing this automatically. I haven’t been working on this for a while, but if anyone is able to come up with a way of, for instance, releasing a specific package while targeting a specific electron version (so other people that install your package don’t need to do the same thing), let me know.