Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron: Using a module in the main process

I'm trying to use a module I made myself in the main process in Electron, but for some reason it doesn't work as intended (at all).

In the main process (main.js), I define the module

const connection = require('connection');

That module exports a function called init

var Connection = {
    init: function() {
        ...
    },
    ....
}
module.exports = Connection;

But when trying to call this method, through the main process (main.js) on a webpage. (login.html)

var Connection = require('electron').remote.connection;
Connection.init();

It tells me that Connection is undefined. I know the Connection module I made works, because I've used it just fine in the renderer process.

The reason that I want to use the entire Connection module in the main process, is so that I could keep that connection alive and performing tasks even when the user goes to another page.

I've searched all around but can't seem to find the solution to my problem.

Thank you.

like image 368
Erveon Avatar asked Aug 16 '26 23:08

Erveon


1 Answers

Found the answer, the proper way to use a module through the main process would be to use this:

require('electron').remote.require('connection');
like image 104
Erveon Avatar answered Aug 18 '26 14:08

Erveon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!