Skip to content
Advertisement

Cannot get my main and renderer processes to communicate in electron

So, I am pretty new to Electron and wanted to build a desktop application. But I have been running into issues, particularly in getting the renderer and main processes to communicate. I am aware of the IPC and remote concepts and this about me not being able to use them in the first place. I have tried to surf through a lot of related questions before deciding to post here. To be more specific, I have a form(HTML) that needs to be filled out and saved to a local database(sqlite) which I originally planned to access from within the HTML but couldn’t. Therefore, I went for a renderer.js and included that as a script to the HTML, which also failed(couldn’t use ‘require’)- I had nodeintegration turned on during both times. Here are the other solutions tried with no luck so far:

  1. A preload.js script : From what I imagined, I would be able to include my ‘require’ statements here but the problem came when I tried to access DOM elements since I had to register the IPC events

  2. I finally went for the browserify tool after learning that it could bundle all necessary modules and make them available to the renderer. Here too, I followed the procedures stated (https://github.com/browserify/browserify#usage), but just couldn’t get it to work with a whole bunch of new errors being thrown (TypeError: fs.existsSync is not a function, RangeError) and I am still getting the dreaded ‘require’ is not defined error in the browser.

I am basically at an impasse now and don’t know where to go from here. I can share some code here if necessary. Any help will be greatly appreciated.

main.js

JavaScript

register.html: This is where I first wanted to save form data to sqlite database

JavaScript

register.js(renderer): require is not defined

JavaScript

preload.js : when i try to access the DOM components here, it complains of being null that is why I tried adding the require(‘./register)…that didn’t work either

JavaScript

There is also of course the bundle.js file from browserify.

Advertisement

Answer

I finally managed to access the required modules and the DOM elements from within the renderer.js file after carefully creating a new file, starting out with the bare minimum and adding more code incrementally to isolate where the problem was. To be perfectly honest, I did not do anything much differently other than get rid of the preload script, turn on nodeIntegration again and like I mentioned, create a new renderer file and link that to the HTML as the script. That did the trick and now the main and UI can communicate through IPC. Since this is merely a desktop application, I am hoping any security issues associated with turning on nodeIntegration won’t be much of a problem.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement