Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Article Comments #1

Open
Pjaerr opened this issue Feb 21, 2020 · 7 comments
Open

Article Comments #1

Pjaerr opened this issue Feb 21, 2020 · 7 comments
Assignees

Comments

@Pjaerr
Copy link
Owner

Pjaerr commented Feb 21, 2020

Comments for the article at http://joshuaj.co.uk/blog/building-desktop-app-svelte-electron

@Pjaerr Pjaerr self-assigned this Feb 21, 2020
@Wolsten
Copy link

Wolsten commented Mar 11, 2021

Hi Joshua,

I found this to be very useful and got it working to the point just before you go onto file saving. However, when I added the following line to App.svelte:

const { ipcRenderer } = require("electron");

The Electron window comes up blank instead of showing the HTML content of App.svelte, which had been working perfectly up to that point. I didn't start adding the other code yet that makes use of the ipcRenderer.

Any suggestions?

Thanks

Steve

@Pjaerr
Copy link
Owner Author

Pjaerr commented Mar 11, 2021

@Wolsten 👋 Hey Steve

I don't seem to get this issue (on windows or mac).

When you create your new BrowserWindow in the electron main.js file, make sure you're setting nodeIntegration to true like so:

const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
    },
  });

if you don't add that it will most likely error as we are trying to use nodejs in the svelte file when we call require("electron")

If that isn't the issue, you can try opening dev-tools when launching the app by adding the following line below where you create your new BrowserWindow

mainWindow.webContents.openDevTools();

which will open the browser dev tools for you when you next run npm start. It's worth checking to see if there is an error message in the console that you can use to help debug.

Give me a shout if you still have an issue and I'll be happy to help!

thanks,
Josh

@Wolsten
Copy link

Wolsten commented Mar 14, 2021 via email

@Wolsten
Copy link

Wolsten commented Apr 27, 2021

Hi Joshua,

Apologies for the delay but I displayed the web console as suggested and saw the following error:

Uncaught ReferenceError: require is not defined
at N (App.svelte:6)
at T (index.mjs:1474)
at new target (main.js:3)
at main.js:3
at main.js:4

This is the line causing the problem:

const { ipcRenderer } = require("electron");

In my electron main.js I am creating the browser window as follows:

const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

This all looks correct?

Best wishes

Steve

@Pjaerr
Copy link
Owner Author

Pjaerr commented Jul 12, 2021

@Wolsten Hey Steve, sorry for not getting back to you sooner!

I think your issue might be related to a security change in Electron (https://www.electronjs.org/docs/breaking-changes#default-changed-contextisolation-defaults-to-true). You may be able to fix by setting contextIsolation: false in your window's webPreferences object.

This may fix your issue but is not the reccomended way to do things as you could technically expose full access to nodejs. I am planning on updating my blogpost soon as I am technically spreading insecure practices but until then I think this github issue comment might be useful for you: electron/electron#9920 (comment)

Thanks,
Josh

@Pjaerr
Copy link
Owner Author

Pjaerr commented May 14, 2022

👋 Hey @Wolsten I realise this is just a tiny bit late in the day but just incase you never found what you were looking for I've updated the blogpost so that it follows the better security practices and shouldn't have the error you were encountering.

I've also updated the template: https://github.com/Pjaerr/svelte-electron-template

PS: Sorry for leaving you hanging with an error like that, I wanted to fix my blogpost up but I had issues getting it to build and started encountering loads of other problems so I just had to rebuild my site from scratch 🙈 I don't expect you to find this blogpost useful anymore given so much time has passed but I thought it worth updating you in any case.

@Wolsten
Copy link

Wolsten commented May 15, 2022

Hi Joshua,

No problem. I am working on some others things at the moment - including brewforukraine.uk - but will keep this in mind for later. I still like the idea of electron except for its bundle size when distributing. Have you looked at neutralinojs? Much smaller as it doesn’t bundle chromium but still at early stages.

Steve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants