colinskow / angular-electron-dream-starter

:tada: An Angular Electron Starter kit featuring Webpack, Angular 4 (Router, Http, Forms, Services, ngrx, Tests, E2E, Coverage), Karma, Spectron, Jasmine, Istanbul, and TypeScript
MIT License
162 stars 54 forks source link

Wrong .node module (native binding) included in build #13

Open greg9504 opened 6 years ago

greg9504 commented 6 years ago

I've recently pulled the upstream changes from this repository. Running into a problem when I attempt to do any of the following: npm start npm run build:prod npm run build:aot

The included build of a native node module (better-sqlite3 in this case) is not correct and so the app fails while loading it with exception:

Uncaught Error: A dynamic link library (DLL) initialization routine failed. \?\D:\src\TAnalyzeElectron\dev\node_modules\better-sqlite3\build\Release\better_sqlite3.node at process.module.(anonymous function) [as dlopen]

Builds built with: npm run build:dev and run with npm run launch work.

Here is what I've done to try to correct it: delete the node_modules directory delete the node_modules_electron directory npm install .\node_modules.bin\electron-rebuild.cmd -f -w better-sqlite3 npm run install-app-deps

the .\node_modules.bin\electron-rebuild.cmd -f -w better-sqlite3 rebuilds the native dependency and when I look in TAnalyzeElectron\node_modules\better-sqlite3\build\Release the .node file appears to be built correctly. And if I manually copy this .node file to any of the builds that don't work, they work.

When I look at the file in TAnalyzeElectron\node_modules_electron\node_modules\better-sqlite3\build\Release the .node file there is different in size and not correct.

The start, build:prod, build:aot etc builds seem to copy the .node file from under the node_modules_electron directory, while the build:dev build appears to take it from under the node_modules directory.

From what I can see the install-app-deps script is supposed to rebuild the native modules for me, and I believe it did this for me before I updated. However it appears there have been no changes to this script. So I'm wondering where to look for the failure? What would cause this script to build the module for the wrong Electron/OS? Do you think this is a problem with electron-builder? I'm on Windows.

I'm working around the problem by copying in the correct build to node_modules_electron\node_modules\better-sqlite3\build\Release, then npm start, npm run build:prod etc get the right .node module.

Thanks for any insight.

colinskow commented 6 years ago

Try npm run install-app-deps. This will rebuild all native dependencies for your current electron version inside node_modules_electron. This should be running automatically as a postinstall step, but may not re-run with npm update.

colinskow commented 6 years ago

Also make sure any native node dependencies are listed under dependencies in your package.json. The build will fail if they are under devDependencies.

greg9504 commented 6 years ago

Hi Colin I tried npm run install-app-deps as noted in the original post above, also yes the module is under dependencies:

"dependencies": { "better-sqlite3": "^4.0.3", "electron-devtools-installer": "^2.2.0", "node-gyp": "3.6.2" },

(Note better-sqlite3 requires node-gyp to be installed global for it's build to work, however the only way I could get the install-app-deps script to work was to have node-gyp installed local, as that's where the better-sqlite3 build seemed to look for it.)

The problem seems to be with how it is built not that it isn't being rebuilt. It's not being built for the OS/platform I'm on (Windows x64). I see the build run when install-app-deps is executed but the resulting .node file can't be loaded. If I manually rebuild using Electron-rebuild with: .\node_modules.bin\electron-rebuild.cmd -f -w better-sqlite3 the .node file produced is correct for the OS/platform. I then just need to copy the .node files to the node_modules_electron/node_modules directory:

"copybettersqlite": "ncp node_modules/better-sqlite3/build/Release/ node_modules_electron/node_modules/better-sqlite3/build/Release/",
"copyinteger": "ncp node_modules/integer/build/Release/ node_modules_electron/node_modules/integer/build/Release/",
"copyallbettersqlite": "npm run copybettersqlite && npm run copyinteger"

Again previously I don't recall having to use electron-rebuild myself and the build that was done by install-app-deps produced a usable .node file.

tjpeden commented 6 years ago

I am having a similar issue with sqlite3. The install-app-deps script doesn't seem to be working correctly

greg9504 commented 6 years ago

@tjpeden what platform are you on? Also were you able to work around it using electron-rebuild and the copy scripts? first install electron-rebuild then: .\node_modules.bin\electron-rebuild.cmd -f -w better-sqlite3

then run copyallbettersqlite from above post.

colinskow commented 6 years ago

Do you have the latest version of config/install-app-deps.js in your repo? It seems this may be an issue with the sqlite modules, possibly because it insists on global node-gyp, whereas you need to make sure it is using the Electron config.

Definitely send a PR if you can figure out how to get it working correctly with simple changes.

tjpeden commented 6 years ago

I know that elentron-builder's install-app-deps cli command builds sqlite3 without issue, I used it on a different seed a couple days ago, but I like this seed better.

colinskow commented 6 years ago

And I've simply built a wrapper around electron-builder's install-app-deps command which builds only package.json dependencies in the node_modules_electron folder. If you can figure out what is causing the failure please let me know.