question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IPC with Vite / svelte-kit

See original GitHub issue

Hello! Thanks for such a great template! 🚀

I cannot seem to get interprocess communication working. Now that remote has been depreciated, in a project I have using Vue/webpack everything is converted to IPC. All good.

At first I thought it may be because of using an older version of this template, but persists after scaffolding a new project.

Possibly related issue vite-electron-builder #189

How can I get IPC to work with Svelte-Kit? Here is an error I am getting:

[svelte] 2:21:17 PM [vite] Error when evaluating SSR module /Users/redacted/Developer/redacted-dev/redacted/src/routes/__layout.svelte:
[svelte] TypeError: Cannot create proxy with a non-object as target or handler
[svelte]     at nodeRequire (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68214:12)
[svelte]     at ssrImport (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68164:20)
[svelte]     at eval (/Users/redacted/Developer/redacted-dev/redacted/src/routes/__layout.svelte:9:31)
[svelte]     at instantiateModule (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68197:166)
[svelte] Cannot create proxy with a non-object as target or handler
[svelte] TypeError: Cannot create proxy with a non-object as target or handler

I have not yet changed the webPreferences object from the template default.

webPreferences: {
  enableRemoteModule: true,
  contextIsolation: true,
  nodeIntegration: true,
  spellcheck: false,
  devTools: dev,
}

WebPrefs in my other project for comparison:

const webPrefs = {
    nodeIntegration: false,
    enableRemoteModule: false,
    contextIsolation: false
 }

Is the issue with the main process or renderer?

if I pass

// electron.cjs
mainWindow.webContents.send('my-channel', 'message')
…

in renderer (say in __layout.svelte):

import { ipcRenderer } from 'electron'

ipcRenderer.on('my-channel', (event, message) => {
  console.log(message) // Prints 'message'
})

Then I get the error mentioned above. Thanks for any insight and if this is beyond the scope of the template project then please accept my apologies.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
bjbkcommented, May 31, 2021

The response from main process can be wrapped in a browser check. See: https://kit.svelte.dev/faq

// preload.cjs

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld(
  'api', {
  send: (channel, data) => {
    ipcRenderer.send(channel, data)
  },
  sendSync: (channel, data) => {
    ipcRenderer.sendSync(channel, data)
  },
  receive: (channel, func) => {
    ipcRenderer.on(channel, (event, ...args) => func(...args))
  }

Main process:

// electron.cjs

…
ipcMain.on('to-main', (event, args) => {
  return mainWindow.webContents.send('from-main', 'hi from main');
})
…

in Svelte component:

<script>
import { browser } from '$app/env'

let desktop;

if (browser) {
  window.api.receive('from-main', (data) => {
    console.log(data);
    desktop = `Received ${data} from main process`;
  });
}

const handleClick = () => {
  window.api.send('to-main', 'a message');
}
</script>

https://stackoverflow.com/a/59888788/4727183

0reactions
bjbkcommented, May 31, 2021

@FractalHQ this seems to solve the problem. Not an issue with the template, but rather just proper implementation. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQ • SvelteKit
Please see the Svelte FAQ and vite-plugin-svelte FAQ as well for the answers to questions deriving from those libraries. How do I use...
Read more >
How To Use SvelteKit With Electron - JavaScript in Plain English
I have almost completed my idea of creating 3 templates for Svelte & Electron. There are still some details to fix but the...
Read more >
Anyone Done Sveltekit + Electron? Its amazing during dev, but ...
If I cant get it working I'll switch, but I have a fair bit of ipc logic that would need to be ported...
Read more >
svelte-web3 - npm
web3.js as a collection of stores for Svelte, Sapper or SvelteKit. ... doesn't play well with bundlers (Vite, Rollup, Webpack, Snowpack, ...
Read more >
Using environment variables in SvelteKit (and Vite)
Tagged with svelte, sveltekit, javascript, webdev. ... Vite statically replaces any use of an environment variable so import.meta.env.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found