4

When I run "yarn run electron-dev" in the CLI, opening http://localhost:3000 in the browser is showing the homepage, but the electron app is showing blank.

Unlike similar issues where other people are having issues with production but working in development, I am having the opposite issue.

Looking at the devtools there are no errors in the browser console, and also nothing is coming up in the console even when I do console.log().

electron.js

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const edge = require('electron-edge-js');
const path = require('path');
const isDev = require('electron-is-dev');
require('electron-reload')

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
      width: 900, 
      height: 680,
      webPreferences: {
        nodeIntegration: true,
      },
  });
  console.log('isDev', isDev);
  mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
  if (isDev) {
    // Open the DevTools.
    //BrowserWindow.addDevToolsExtension('<location to your react chrome extension>');
    mainWindow.webContents.openDevTools();
  }

  mainWindow.on('closed', () => mainWindow = null);
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
    console.log('entering func123');
    func123();
  }
});

Package.json

{
  "name": "myelectronapp",
  "description": "Electron + Create React App + Electron Builder",
  "version": "0.1.0",
  "private": true,
  "author": {
    "name": "Andrew C",
    "email": "your.email@domain.com",
    "url": "https://your-website.com"
  },
  "build": {
    "appId": "com.andrewchoi.my-electron-app",
    "productName": "MyElectronApp",
    "copyright": "Copyright © 2019 ${author}",
    "mac": {
      "category": "public.app-category.utilities"
    },
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
    "directories": {
      "buildResources": "assets"
    }
  },
  "dependencies": {
    "@material-ui/core": "^4.9.4",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "electron-edge-js": "^12.8.1",
    "electron-is-dev": "^1.1.0",
    "mssql-connection-string": "^0.3.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.4.0"
  },
  "main": "public/electron.js",
  "homepage": "./",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "rescripts test",
    "eject": "react-scripts eject",
    "electron-dev": "concurrently \"cross-env BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
    "postinstall": "electron-builder",
    "preelectron-pack": "yarn build",
    "electron-pack": "electron-builder build -w"
  },
  "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"
    ]
  },
  "devDependencies": {
    "@rescripts/cli": "^0.0.13",
    "@rescripts/rescript-env": "^0.0.11",
    "concurrently": "^5.1.0",
    "cross-env": "^7.0.0",
    "electron": "8.0.1",
    "electron-builder": "22.3.5",
    "wait-on": "^4.0.0"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>
Icecubelegacy
  • 189
  • 1
  • 4
  • 14
  • Mind sharing your `index.html` file. It might be missing `.` before base href? – Pramesh Bajracharya Feb 25 '20 at 05:42
  • It works for me when I remove electron-reload from electron.js. electron-reload is not listed in your package.json. I'm also having an issue with electron-edge-js. If I remove those 2 dependencies I can load a blank index.html file on localhost:3000. – narmageddon Feb 25 '20 at 22:47
  • @PrameshBajracharya I used create-react-app for this app and haven't changed those files, so I didn't think it would be the issue, but I've included it now in the question. – Icecubelegacy Feb 25 '20 at 22:58
  • @narmageddon I tried removing the two dependencies electron-reload and electron-edge-js but I was still getting blank electron app – Icecubelegacy Feb 25 '20 at 22:59

2 Answers2

2

This looks strange to me:

mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);

Is there a reason to use a separate server for the client content instead of just passing the index file as a file resource just like in prod?

Check that this other server is running. I know the 'blank page' effect when I try to load web url but the server is not running.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Presumably the `wait-on` (see the `electron-dev` entry in package.json) is supposed to make sure the port 3000 server is running. (But, I agree, this is the line where the "code smell" is strongest, the mismatch between file:// and http:// ) – Darren Cook Feb 25 '20 at 08:25
  • I think it makes it just more complicated and I'd be happy to learn the benefit from that setup. I think, if you serve the client resource from a server in dev and from files in prod, you just have to be extra careful with the packaging and that you can require in everything you need from 'electron' in your client side scripts in that development mode. – Andreas Dolk Feb 25 '20 at 09:29
  • @Andreas_D I tried just removing the localhost url so it loads the index file, but it was still not working. – Icecubelegacy Feb 25 '20 at 23:00
  • I'm facing the same problem as him. And I just want to mention why he loads the URL instead of loading the index file, is that I guess he wants to develop the app in an electron environment and he tries to avoid the building process of the frontend whenever he changes backgroundColor, which in my case the build takes up to 30s. – Medi Apr 13 '21 at 13:41
0

This issue has been resolved. I removed electron-edge-js and electron-reload, and went back to using npm instead of yarn to run the scripts and install dependencies, etc. And now it is working for both dev and prod.

Icecubelegacy
  • 189
  • 1
  • 4
  • 14