Hi all,
I'm trying to package an Electron application with a custom native addon (i.e. written by me), and have been unsuccessful so far. The app builds, but when I run it, it's unable to load the native module, and exits.
Running the app locally, with electron . works just fine, which leads me to believe that the native module has not been packaged with the executable.
My package.json looks like this:
{
"build": {
"appId": "App name",
"productName": "App company",
"copyright": "Copyright 漏 2017 App Inc.",
"nsis": {},
"win": {
"target": "nsis"
},
"mac": {
"target": "dmg"
}
},
"scripts": {
"pack": "electron-builder --dir",
"dist": "electron-builder -w --x64 --ia32"
},
"postinstall": "electron-builder install-app-deps",
"devDependencies": {
"electron-builder": "^19.6.3"
}
}
And the app directory structure like this:
application-root-dir
-app
-addon
-build
-Release
-addon.node
-addon.sln
-node_modules
-main.js
-package.json
-build
-node_modules
-package.json
Addon source code and MSVC project files, as well as *.node files are all in application-root-dir/app/addon/*
When I run npm run dist, I get the following output:
> electron-builder -w --x64 --ia32
electron-builder 19.35.1
No native production dependencies
Packaging for win32 x64 using electron 1.6.14 to dist\win-unpacked
No native production dependencies
Packaging for win32 ia32 using electron 1.6.14 to dist\win-ia32-unpacked
Building nsis installer (x64 and ia32)
So, everything seemingly builds just fine, and the app installs successfully, but when I try to run, it's unable to load the native module (from main.js), and just exits. Below is the manner in which I load/require the module:
var addon
try {
addon= worker (
path.join(__dirname, './addon/build/Release/addon'),
{name: 'addon'}
);
}
catch(err) {
try {
addon= worker (
path.join(__dirname, './addon/build/Debug/addon'),
{name: 'addon'}
);
}
catch(err) {
log.error('Unable to load addon, exiting app...')
app.exit()
}
}
Any help would be appreciated.
Settings "nodeGypRebuild":"true" in root package.json solved above issue.
Does not work for me though. It says Executing node-gyp rebuild for arch x64 during build because of that option set in config but packaged app contains no *.node whatsoever.
A section of asar listing where build/Release/*.node should be
...
/.cache/4e1a5069de66f1b2f52927c2388cdfb0756c1285/dbbf1d131492e21ad126f690015bc5785a84e466
/binding.gyp
/package.json
/node_modules
/node_modules/ansi-regex
...
$ electron-builder --version
19.47.1
@edudar yeah, it worked for me the first time around, and then stopped working after that. Ended up ditching Electron in the end, so there's that.
Using debug output I got electron-builder-debug.yml where there's a line that precisely excludes everything in build directory from being packaged. That is not the line from package.json provided so I assume these are auto-generated by the builder itself. Not sure how this was supposed to work at all...
x64:
firstOrDefaultFilePatterns:
- '**/*'
- node_modules/**/*
- '!build{,/**/*}'
- '!dist{,/**/*}'
@nikola-matic maybe reopen this ticket then?
So the workaround is simple as I appended && mkdir ./Release && cp ./build/Release/addon.node ./Release to my install script as Release is a potential place where addons are looked up at and it's not excluded from packaging.
@nikola-matic Did you solve this problem? Same for me. Please help!
@Cprofessor3 unfortunately not; in the end, we moved away from electron and used CEF instead.
@nikola-matic Thanks!
@nikola-matic Hi, luckly I find out how it works. Two points. Firstly, electron-builder will ignore [builid/] when building exe. So I renamed [build] to [out], and then require addon from [out]. Secondly, all the [.dll] files should placed in the folder where you-app is placed in. {"extraFiles": ["/.dll"]} in package.json in "build" will work. Hope it helpful.