How to access mainWindow outside the main.js file

I am working on a project using electron and reactjs. I use the IPCRenderer and IPCMain to trigger database calls between the frontend and the backend.

In order not to make the main.js file too long, I moved the code for ipcMain.on calls to another(ipcMainHandlers.js) file in another directory and require the file in the main.js file.

The challenge I have is how to access the mainWindow from inside that file. I tried adding the mainWindow to globals.shared, but it comes back with undefined. I have tried using remote, but that returns undefined for remote.

const path = require('path');
const url = require('url');

const { app, BrowserWindow } = require('electron');

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
      preload: __dirname + '/preload.js'

  const startUrl =
    process.env.ELECTRON_START_URL ||
      pathname: path.join(__dirname, './build/index.html'),
      protocol: 'file:',
      slashes: true,

  mainWindow.on('closed', function () {
    mainWindow = null;

app.on('ready', createWindow);
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
app.on('activate', function () {
  if (mainWindow === null) {


This is the ipcMainHandler file

const AuthController = require('../controller/Auth.controller');
const { SIGNIN_REQUEST, SIGNIN_RESPONSE } = require('../../common/events.js');
const { ipcMain, remote } = require('electron');


const currentWindow = remote.getCurrentWindow();

ipcMain.on(SIGNIN_REQUEST, (event, data) => {
  const user = AuthController.userSignin({ });
  currentWindow.send(SIGNIN_RESPONSE, user);

Any help is greatly appreciated.

This has been solved. I resolved it by using the event object.

ipcMain.on(SIGNIN_REQUEST, async (event, data) => {
  const user = await AuthController.userSignin({ });
  event.sender.send(SIGNIN_RESPONSE, user);