Closed
Description
I am trying to webpack all the main.js script & its dependencies in a single file (I want to have one file for the UI app, and one file for the server app).
If I use the normal source, index.html loads just fine. However, when webpacking, I get the error mentioned in the title.
Here's the webpack config I used:
webpack({
entry: [
'./main'
],
output: {
path: path.join(__dirname, 'asar'),
filename: 'main.js',
libraryTarget: 'commonjs2'
},
externals: ['electron'],
target: 'node'
});
I load the file like this:
mainWindow.loadURL('file://' + __dirname + '/index.html');
I think that its perhaps due to webpack calling/evaling stuff outside of the electron context that allows serving local files.
Any ideas/suggestions? Thanks!
- Electron version: 0.37.2
- Operating system: Windows 10 Home
Activity
zcbenz commentedon Apr 11, 2016
You can probably try turning off
webSecurity
inwebPreferences
ofBrowserWindow
. For more questions I suggest seeking help from the community.singhshashi commentedon Jun 25, 2016
@MihaiValentin Hey, did you find a solution for this?
eiriarte commentedon Jun 29, 2016
@MihaiValentin @singhshashi I had the same problem. It seems webpack, by default, tries to "mock" Node globals like
__dirname
. I disabled this behavior with thenode.__dirname
option and… it worked!Just in case, I'm also using webpack-target-electron-renderer for the
target
option.This is my config so far. I hope it helps:
singhshashi commentedon Jun 30, 2016
@eiriarte Thanks for sharing that, however that did not work. If I pack the files for the main process using webpack, even with the node configuration that you have said, I still get the same error.
I am sharing a workaround that I am using to get around the issue for others who land on this thread.
Instead of using es features which require babel to transpile them to work correctly in the main. js file, I separated these out into different files. In my main.js I do not use any features which require babel transpilation. So instead of import I use require. For code which was using es7 proposal features such as async, I moved that code to different files, within a folder called desktopServices (you could come up with a better name). I now run webpack to create a bundle for desktopServices and I reference this bundle in main.js.
const myshell = require('./dist/desktopServices').myShell
;My webpack.config.main.js file contains the following config.
Not the cleanest way, but I guess this is the route I am taking for the time being till some of the es features I want to use get incorporated into node and don't require babel transpilation.
rob3c commentedon Dec 13, 2016
For me, it turned out to be a misleading error. I was getting the
not allowed to load local resource
error because webpack hadn't finished writing the file before I was trying to load it, not because it was there with the wrong permissions.I
fixedwrapped it withsetTimeout
(the duct tape of javascript) so I could get on with life:milewski commentedon Dec 13, 2016
for me.. the reason was because the path the webpack was outputting the bundle.. was out of reach... i solved it by coming back a few directories and applying the node config as suggested above.. works perfectly :D
j-peterson commentedon Feb 20, 2017
FYI to those here via google: you get the same error if the file doesn't exist. I forgot to tell electron-packager to copy the target file into the app. Learn from my stupid mistakes :)
popey456963 commentedon May 8, 2017
For future reference (as I've searched through this page too many times), here are the current possible problems:
The file doesn't exist, or your Node application can't reach it. Make sure
electron-packager
is copying the target file into the app!You might need to disable
webSecurity
withinwebPreferences
when you create yourBrowserWindow()
:node.__dirname
, you can disable this by adding the following to your config:setTimeout()
(not recommended) or waiting for theready
event being sent from the app (better).ishwarrimal commentedon May 24, 2017
For me the solution was
Hope this helps someone nube like me.
17 remaining items
fatalmuffin commentedon Jun 14, 2018
Just thought I'd add, I also got this error due to my own blunder (cap sensitivity)
I was calling
pathname: path.join(__dirname, 'Views/settingsWindow.html')
when the file name was all lower case.This only casued an error once it was webpacked.
it-arjan commentedon Jan 7, 2019
I tried some of the solutions but couldn't get it to work (using angular@7.x with electron@3.0.7).
I found the best solution in a post with only 3 votes on SO: Turns out there is no need for this package!
https://stackoverflow.com/questions/45041364/angular-electron-webpack-live-reloading
Zero config-hassle solution:
-npm uninstall electron-reload
-Run ng serve in one terminal
-in main.js change win.loadURL(
http://localhost:4200/index.html
);-then run npm run electron in another terminal
It just WORKS
tyrants666 commentedon Apr 21, 2019
I Tried to fix this my whole day & finally this guys solution worked do check
electron-userland/electron-builder#2955 (comment)
proton1k commentedon Jun 3, 2019
When you define the "build" attribute in the package.json, just add required files as following:
Then the electron-builder will pack it correctly.
TheDigiBusiness commentedon Dec 4, 2019
Turned out for that the 'file://' prefix was all i needed for the loadUrl method.
Had:
win.loadUrl(path.join(__dirname, "./index.html"))
Replaced with:
win.loadUrl(path.join("file://",__dirname, "./index.html"))
bonniss commentedon Jun 8, 2020
Webpack baffles me mixing both forward and backward slashes in the URL to the html entry, so I use node's
url
andpath
to make it work:cnscorpions commentedon Dec 8, 2020
it is a disaster, I am stuck in CRA + electron 😂, running in dev mode is okay, but packaged into windows exe does not work at all.
cnscorpions commentedon Dec 8, 2020
I got it. 🤣 If you use CRA with react-router, you should use HashRouter, not BrowerRouter. DONE!!! 😂 refer to electron-userland/electron-builder#2167