Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

441
Vistas
Electron: Sending app.getPath data from the main process to the renderer process

Given:

webPreferences: {
  nodeIntegration: false,
  contextIsolation: true,
  sandbox: true,
}

I need to access the result of app.getPath("appData") in my renderer process.

app.getPath("appData") only works in the main process.

contextBridge.exposeInMainWorld() only works on the preload script.

How do I expose the result of app.getPath("appData") to the renderer if the preload script can't access app.getPath("appData") and the main process can't access contextBridge.exposeInMainWorld()?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can

  • set up the preload script to call exposeInMainWorld to put a Promise-invoking function on the window. When invoked, make a request to the main process
  • set up the main process to respond to requests from the preload script with the desired info
// Main process:
ipcMain.handle('getPath', () => app.getPath("appData"));
// Preload:
contextBridge.exposeInMainWorld('electronAPI', {
    getPath: () => ipcRenderer.invoke('getPath')
});
// Renderer:
window.electronAPI.getPath()
  .then((appDataPath) => {
    // ...
  })
  .catch(handleErrors);

The preload script essentially acts as a bridge between the sandboxed page and the privileged main process - often, most of an application's logic will live either in the main process or the renderer, and the preload only bridges them.

For a more generalized approach of sending messages from the renderer to main (possibly while waiting for a response), I've found the following to work pretty well:

// Preload:
contextBridge.exposeInMainWorld('electronAPI', {
    sendMessageToMainProcess: (channel: string, payload: unknown) => ipcRenderer.invoke(channel, payload),
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda