skip to Main Content

With the executable (yarn make) I get this error :

electron: Failed to load URL: http://localhost:3000/main_window with error: 
ERR_CONNECTION_REFUSED

This error does not show up in the development mode

I tried to differentiate between dev and prod modes in main :

 import isDev from 'electron-is-dev' 
 const createWindow = (): void => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
    webPreferences: {
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY
    }
  });

  // and load the index.html of the app.
  isDev ? mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY) : `file://${path.join(__dirname, '../build/index.html')}`

  // Open the DevTools.
  if (isDev ) {
    mainWindow.webContents.openDevTools();
  }
};

but in this way the error message doesn’t show up, but also the correct window’s page Electron App doesn’t show up :

In Development Mode : "Got Finally!!" message in the window’s page is present,

enter image description here

but not in Production Mode

enter image description here

meaning that the page is not correctly loaded

Other Info:

Electron: 19
O.S. : Ubuntu 22.04

How to make it work?

Update 1)

Thanks to @Bets comment I realized I made two mistakes (the folder and the ternary operator) and I updated the line of code accordingly:

mainWindow.loadURL(isDev ? MAIN_WINDOW_WEBPACK_ENTRY : `file://${path.join(__dirname, '../renderer/main_window/index.html')}`)

The folder this time is correct:

console.log(path.join(__dirname,'../renderer/main_window/index.html')) =>
/home/raphy/ForgeTypescriptReactWebpack
/out/forgetypescriptreactwebpack-linux-x64/resources/app/.webpack
/renderer/main_window/index.html

raphy@raohy:~/ForgeTypescriptReactWebpack/out/forgetypescriptreactwebpack-linux-x64/resources/app/.webpack/renderer/main_window$ ls -lah
total 3,5M
drwxrwxr-x 2 raphy raphy 4,0K gen  4 12:17 .
drwxrwxr-x 3 raphy raphy 4,0K gen  4 12:17 ..
-rw-rw-r-- 1 raphy raphy  386 gen  4 12:17 index.html
-rw-rw-r-- 1 raphy raphy 3,4M gen  4 12:17 index.js
-rw-rw-r-- 1 raphy raphy 1,8K gen  4 12:17 preload.js

but, still, the page is not correctly loaded and not getting the right output

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to electron-forge people, who answered my help-request in Discord, I solved the issue with these two steps:

    1. I upgraded all @electron-forge/* dependencies to latest

    2. in main : mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY)


  2. It looks like your code for loading the page is causing the issue, it should be:

    // and load the index.html of the app.
    mainWindow.loadURL(isDev ? MAIN_WINDOW_WEBPACK_ENTRY : `file://${path.join(__dirname, '../build/index.html')}`)
    

    That is, the ternary operator should determine what path/URL to load as the argument for the mainWindow.loadURL function call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search