2

We have an electron app and want to load the renderer bundle using loadUrl().

win=new BrowserWindow({ /* ... */ })
win.loadURL(`file://${path.join(__dirname, '../../render/build/index.html')}`);

Inside the html file, we load the React Bundle

<script type="text/javascript" src="/js/app-my-hash.bundle.js"></script>

However, as expected, the file is not found, since, I guess, I need to set the root of the project somehow. I get this error

Failed to load resource: net::ERR_FILE_NOT_FOUND

What setting am I missing on the electron side (or webpack side) to get this working?

Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62
  • Which file is not found? If index.html, does it load if you use absolute address instead of path.join? Or app-my-hash.bundle.js ? If yes, Is it in root js folder? Did you try to load it with require('myfile.js')? – Nemi Apr 10 '19 at 18:36
  • The JS is missing. It is not in root js. It is in a js folder that is relative to the index.html. I did not try to load it with require, it is bundled by webpack. – Andrei Cioara Apr 10 '19 at 18:50
  • 2
    Try relative URL, like: `src="js/app-my-hash.bundle.js"` or `src="./js/app-my-hash.bundle.js"` – Nemi Apr 10 '19 at 18:58
  • Yup. That worked. – Andrei Cioara Apr 10 '19 at 21:02
  • Great. I put it as answer. – Nemi Apr 10 '19 at 22:48

2 Answers2

3

Another approach to add relative path without manual update in index.html is by including,

homepage property in package.json.

For an example,

{
  "name": "electron-project",
  "homepage": "./", # Add this line
  #.... all remaining properties
}

Stackoverflow Source

Yogi
  • 1,527
  • 15
  • 21
1

Use relative URL, like: src="js/app-my-hash.bundle.js" or src="./js/app-my-hash.bundle.js"

Nemi
  • 1,012
  • 10
  • 19