Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

440
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!