14

I've tried to import ipcRenderer in react app

import {ipcRenderer} from 'electron';

but I get this error message : require is not defined

Vito
  • 320
  • 1
  • 3
  • 11

6 Answers6

23

You'll want to follow the steps I outline in this comment. These steps ensure security in your electron app.

main.js

const {
  app,
  BrowserWindow,
  ipcMain
} = require("electron");
const path = require("path");
const fs = require("fs");

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;

async function createWindow() {

  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      enableRemoteModule: false, // turn off remote
      preload: path.join(__dirname, "preload.js") // use a preload script
    }
  });

  // Load app
  win.loadFile(path.join(__dirname, "dist/index.html"));

  // rest of code..
}

app.on("ready", createWindow);

ipcMain.on("toMain", (event, args) => {
  fs.readFile("path/to/file", (error, data) => {
    // Do something with file contents

    // Send result back to renderer process
    win.webContents.send("fromMain", responseObj);
  });
});

preload.js

const {
    contextBridge,
    ipcRenderer
} = require("electron");

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
    "api", {
        send: (channel, data) => {
            // whitelist channels
            let validChannels = ["toMain"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            let validChannels = ["fromMain"];
            if (validChannels.includes(channel)) {
                // Deliberately strip event as it includes `sender` 
                ipcRenderer.on(channel, (event, ...args) => fn(...args));
            }
        }
    }
);

index.html

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="utf-8"/>
    <title>Title</title>
</head>
<body>
    <script>
        window.api.receive("fromMain", (data) => {
            console.log(`Received ${data} from main process`);
        });
        window.api.send("toMain", "some data");
    </script>
</body>
</html>
reZach
  • 8,945
  • 12
  • 51
  • 97
  • 5
    in preload.js it should read: ipcRenderer.on(channel, (event, ...args) => func(...args)); – stanek May 13 '21 at 22:15
21

You need to use

const { ipcRenderer } = window.require("electron");

Otherwise it will try to import it from Webpack or whatever module bundler you use.

You can checkout this thread for a better explanation:

https://github.com/electron/electron/issues/7300

Gh05d
  • 7,923
  • 7
  • 33
  • 64
1

if you are looking to quickly try IPC in electron in a react component, this may help.

main.js

const {ipcMain} = require('electron')

ipcMain.on('asynchronous-message', (event, arg) => {
  console.log("heyyyy",arg) // prints "heyyyy ping"
})

App.js

import React from 'react';
import './App.css';
const { ipcRenderer } = window.require('electron');


function App() {
  return (
    <div className="App">
       <button onClick={()=>{
         ipcRenderer.send('asynchronous-message', 'ping')
         }}>Com</button>
    </div>
  );
}

export default App;

Output: output

Don't forget to re-run the app after making the changes.

No preload.js added, Did not change anything in webpack config, No extra configurations.

Aditya Patnaik
  • 1,490
  • 17
  • 27
  • 4
    I'm getting `Uncaught TypeError: window.require is not a function` – Angel S. Moreno Nov 15 '21 at 20:17
  • 1
    @AngelS.Moreno: relevant discussion https://github.com/electron/electron/issues/9920#issuecomment-797143298 – Dan O Nov 16 '21 at 07:34
  • 1
    I am getting Uncaught TypeError: window.require is not a function. Any solution found? – vishnu Mar 20 '22 at 20:05
  • 1
    I have no idea why it works without contexts and preload.js, but it works for me. I use Electron 19. – joba2ca Sep 17 '22 at 16:49
  • Either use `preload.js` or add this `webPreferences: {nodeIntegration: true, contextIsolation: false, enableRemoteModule: true,},` to main.js (or whatever the entry file is) in particular `contextIsolation: false,`. – mikey Mar 30 '23 at 06:23
1

By using contextBridge we can resolve this issue

new BrowserWindow({
    width: 1200,
    height: 800,
    backgroundColor: "white",
    webPreferences: {
      nodeIntegration: false,
      worldSafeExecuteJavaScript: true,
      contextIsolation: true,
      preload: path.join(__dirname, 'preload.js')
    }
  })

//example to display notification
ipcMain.on('notify', (_, message) => {
   new Notification({title: 'Notification', body: message}).show();
})

preload.js

const { ipcRenderer, contextBridge } = require('electron');

contextBridge.exposeInMainWorld('electron', {
  notificationApi: {
    sendNotification(message) {
      ipcRenderer.send('notify', message);
    }
  }
})

then using the below code in your reactjs component will trigger a native notification message

electron
      .notificationApi
      .sendNotification('My custom message!');
Sathishkumar Rakkiyasamy
  • 3,509
  • 2
  • 30
  • 34
0

Below is the code worked for using electron-forge with react with preload script which exposes ipcRenderer API to renderer process in electron with webpack preload(FYI check package.json code below) option. Follow the link to create a project for electron-forge with react.

main.js

const { app, BrowserWindow, ipcMain, Notification } = require("electron");
const path = require("path");

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
  // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  console.log(__dirname, "testing");
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      worldSafeExecuteJavaScript: true,
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
    },
  });

  // and load the index.html of the app.
  mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
  console.log("/n demo", __dirname, "/n");
  createWindow();
});

ipcMain.on("notify", (_, message) => {
  new Notification({ title: "Notification", body: message }).show();
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

package.json

{
  "name": "webex-ui",
  "productName": "webex-ui",
  "version": "1.0.0",
  "description": "My Electron application description",
  "main": ".webpack/main",
  "scripts": {
    "start": "electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make",
    "publish": "electron-forge publish",
    "lint": "echo \"No linting configured\""
  },
  "keywords": [],
  "author": {
    "name": "Sathishkumar R",
    "email": "rsathishtechit@gmail.com"
  },
  "license": "MIT",
  "config": {
    "forge": {
      "packagerConfig": {},
      "makers": [
        {
          "name": "@electron-forge/maker-squirrel",
          "config": {
            "name": "webex_ui"
          }
        },
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin"
          ]
        },
        {
          "name": "@electron-forge/maker-deb",
          "config": {}
        },
        {
          "name": "@electron-forge/maker-rpm",
          "config": {}
        }
      ],
      "plugins": [
        [
          "@electron-forge/plugin-webpack",
          {
            "mainConfig": "./webpack.main.config.js",
            "renderer": {
              "config": "./webpack.renderer.config.js",
              "entryPoints": [
                {
                  "html": "./src/index.html",
                  "js": "./src/renderer.js",
                  "name": "main_window",
                  "preload": {
                    "js": "./src/preload.js"
                  }
                }
              ]
            }
          }
        ]
      ]
    }
  },
  "devDependencies": {
    "@babel/core": "^7.14.8",
    "@babel/preset-react": "^7.14.5",
    "@electron-forge/cli": "^6.0.0-beta.58",
    "@electron-forge/maker-deb": "^6.0.0-beta.58",
    "@electron-forge/maker-rpm": "^6.0.0-beta.58",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.58",
    "@electron-forge/maker-zip": "^6.0.0-beta.58",
    "@electron-forge/plugin-webpack": "6.0.0-beta.58",
    "@vercel/webpack-asset-relocator-loader": "1.6.0",
    "babel-loader": "^8.2.2",
    "css-loader": "^6.0.0",
    "electron": "13.1.7",
    "node-loader": "^2.0.0",
    "style-loader": "^3.0.0"
  },
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}

app.jsx

import * as React from "react";
import * as ReactDOM from "react-dom";

class App extends React.Component {
  componentDidMount() {
    electron.notificationApi.sendNotification("My custom message!");
  }
  render() {
    return <h1>contextBridge</h1>;
  }
}

ReactDOM.render(<App />, document.body);

preload.js

const { ipcRenderer, contextBridge } = require("electron");

contextBridge.exposeInMainWorld("electron", {
  notificationApi: {
    sendNotification(message) {
      ipcRenderer.send("notify", message);
    },
  },
  batteryApi: {},
  fileApi: {},
});

renderer.js

import "./index.css";
import "./app";

console.log(
  ' This message is being logged by "renderer.js", included via webpack'
);

Also, add below in webpack.rules.js

//to avoid explicit mention of jsx when importing react components
resolve: {
      extensions: [".js", ".jsx"],
    },

Thanks to this page

Sathishkumar Rakkiyasamy
  • 3,509
  • 2
  • 30
  • 34
  • I'm getting `'worldSafeExecuteJavaScript' does not exist in type 'WebPreferences'.` using the eletron-forge TS+webpack+react – Angel S. Moreno Nov 15 '21 at 20:13
  • In Electron 14, `worldSafeExecuteJavaScript` will be removed. There is no alternative, please ensure your code works with this property enabled. It has been enabled by default since Electron 12. Please refer the https://www.electronjs.org/docs/latest/breaking-changes. I – Sathishkumar Rakkiyasamy Nov 16 '21 at 07:09
-5

const {ipcRenderer} = require('electron')

Fadi Abo Msalam
  • 6,739
  • 2
  • 20
  • 25