To get the app path at your main process. Then use this code at your main.js
switch(process.platform) { case 'darwin': { return path.join(process.env.HOME, 'Library', 'Application Support', ...); } case 'win32': { return path.join(process.env.APPDATA, ...); } case 'linux': { return path.join(process.env.HOME, ...); }}
And going to get the path from the renderer then use this code at your renderer
const remote = require('electron').remote;const app = remote.app;console.log(app.getPath('userData'));
But to use require at your renderer, please make sure nodeintegration is true.
If I were you, I was going to get the app path at main process and store the file at main process as well.Hence, importing many dependencies at renderer process is not a good choice. The renderer process mainly takes care of showing your app in the Chromium browser.
So to make this operation at main process. Use this
at your main.js
const { ipcMain } = require('electron') const appPath = () => { switch(process.platform) { case 'darwin': { return path.join(process.env.HOME, 'Library', 'Application Support'); } case 'win32': { return process.env.APPDATA; } case 'linux': { return process.env.HOME; } } } const writeToFile = (fileName, inData) => { const fullPath = path.join(appPath(), "\\", fileName); fs.writeFile(fullPath, inData, (err) => { // throws an error, you could also catch it here if (err) throw err; // success case, the file was saved console.log('Lyric saved!'); }); } // write_to_file ipcMain.on('WRITE_TEXT', async (event, arg) => { writeToFile(arg.fileName, arg.inData) });
At your renderer process add this code.
const {ipcRenderer} = require('electron')ipcRenderer.sendSync('WRITE_TEXT',{fileName, inData})
As you can see, at renderer process, this is sending the inp_data
to your main process through 'WRITE_TEXT'IPC channel.
One more thing here, at your code. You are connecting your DB at your renderer and it's possible but this is not a right choice. Please think while you are having the several renderer. You should move this to main process too.