electron-packager not working after run application
I am try to create app for macos with electron-packager. But i get error after run application. at line [https://github.com/ionic-team/capacitor/blob/master/electron/index.js#L11]
node -v v10.0.0
"@capacitor/cli": "^1.0.0-beta.13",
"@capacitor/core": "^1.0.0-beta.13"
How i can fix it?
i an try to debug
mainWindow.loadURL(await injectCapacitor(file://${__dirname}/app/index.html),
__dirname = /Users/sasha/ngonestarter/electron/capacitor-app-darwin-x64/capacitor-app.app/Contents/Resources/app/app/index.html
and this file in application folder.

../electron/capacitor-app-darwin-x64/capacitor-app.app/Contents/Resources/app/node_modules/@capacitor/electron/index.js let rootPath = process.cwd(); rootPath is empty.
i fix it local by code:
const realPath = __dirname.split('/app')[0] + '/app';
this.splashWindow.loadURL(`data:text/html;charset=UTF-8,${splashHtml}`, {baseURLForDataURL: `file://${realPath}/splash_assets/`});
this.splashWindow.webContents.on('dom-ready', async () => {
this.splashWindow.show();
setTimeout(async () => {
this.mainWindowRef.loadURL(await injectCapacitor(`file://${realPath}/app/index.html`), {baseURLForDataURL: `file://${realPath}/app/`});
}, 4500);
});
What is the legal solution of this problem?
Same here with "electron-builder". Applications do start, but crash with the message above. Is there any configuration I'm missing? Thanks for any help :)
@jcesarmobile Hey! So I tried to compile and use this fix, but I am still getting the same error. How can I try to verify this? I must have done something wrong, but... the problem persists.
What I did: Added the following to my app index.ts:
import * as path from 'path';
(global as any).__basedir = path.resolve(__dirname);
Then I did a few build steps:
git clone [email protected]:ionic-team/capacitor.git
cd capacitor
npm i
npm run build
cd /path/to/my/cap/electron/project
npm link ../../capacitor/electron
npm run build
electron-builder --x64 -w
And I still get the toString of undefined error. Not sure what I missed?
Did you run the npm i and npm run build inside electron folder?
Actually yes, I figured out what is going wrong. Partly user error, partly this being sort of a confusing issue that's difficult to debug since it's hard to get log output at this stage of the app (unless I'm missing something).
I've configured my app to put compiled sources into the ./build/* dir, including index.ts/js.
Thus the basedir was incorrect. I resolved it using the following:
(global as any).__basedir = path.resolve(__dirname.replace(/(.*)\/build.*$/, '$1'));
If you put your main file anywhere but the root dir, I guess you'll have to do something like this. I wonder if there's a better way to detect the beginning of the asar filesystem when packaging for native builds.
I spoke too soon. It seems to work on OSX but not in Windows. Will do some more digging later.
Hang on. User error. Typical windows slash/backslash issue...
(global as any).__basedir = path.resolve(__dirname.replace(/(.)build(?!.build.).$/, '$1'));
It works. Splash screen resources not loading but will check on that later... again.
Splash resources are working, but needed to do something like this, based on the default configuration of what the template generated for me:
splashScreen = new CapacitorSplashScreen(mainWindow, { baseURLForDataURL: `file://${__dirname}/splash_assets/` });
Thank you @jcesarmobile, looking forward to the next release :) This project is great, BTW! Huge fan of where this is headed.
Hi @lincolnthree - could you please clarify exactly what changes you made in order for you to be able to run a working app?
@jcesarmobile - If you'd prefer me to create a new issue, pls just let me know.
I'm having exactly same issues with a brand new Capacitor project that I started 2 days ago, with Create React App V3 (on Windows).
Here's the error when trying to run the app, after using electron-builder to package it and running the installer:

Additional info:
- Project root
package.json:
{
"name": "poc-capacitor",
"version": "0.1.0",
"private": true,
"homepage": ".",
"dependencies": {
"@capacitor/android": "^1.0.0-beta.25",
"@capacitor/cli": "^1.0.0-beta.25",
"@capacitor/core": "^1.0.0-beta.25",
"@types/jest": "24.0.13",
"@types/node": "^12.0.2",
"@types/react": "^16.8.18",
"@types/react-dom": "16.8.4",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"typescript": "3.4.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && npx cap copy",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
- Electron
package.json:
{
"name": "capacitor-poc",
"version": "1.0.0",
"description": "POC Capacitor - Electron, Android, iOS",
"main": "index.js",
"author": "",
"license": "ISC",
"keywords": [
"capacitor",
"electron"
],
"dependencies": {
"@capacitor/electron": "^1.0.0-alpha.28",
"electron-is-dev": "^0.3.0"
},
"devDependencies": {
"electron": "^4.0.0",
"electron-builder": "^20.40.2"
},
"scripts": {
"electron:start": "electron ./",
"electron:build": "build -c.extraMetadata.main=app/index.js"
},
"build": {
"appId": "com.omnicogroup.omniui.capacitor.poc",
"artifactName": "${productName}-setup.${ext}",
"files": [
"app/**/*",
"node_modules/**/*"
],
"directories": {
"buildResources": "electron-assets"
},
"nsis": {
"oneClick": true,
"perMachine": true,
"runAfterFinish": false
}
}
}
- I have not modified the default electron
index.jsfile. But I have copied the file into\project-root\public\folder so that when I build my web app and runnpx cap copy, the file gets copied to\project-root\electron\app\folder.
I now have electron-builder working for my CRA app.
- I had issues with starting electron app, which I fixed yesterday - see my Git issue comment.
- Today, I did a clean re-install of electron and electron-builder, and the built app now starts OK.