2

I'm using electron and react, the react app was created with the create-react-app boilerplate. I need to use the electron API to open an external link, but this task is achieve from a react component. I imported the remote electron inside the react component(in order to get access to the current window.) but that gave me an error

const {remote} = require("electron")

The error is raised from electron itself, from node_modules/electron/index.js and the problem is with fs module, it says:

TypeError: fs.existsSync is not a function

This error is showed in the electron windows.

3
  • 2
    github.com/electron/electron/issues/7300 might be a similar issue. Solution was to use window.require('electron') Commented Aug 7, 2018 at 15:34
  • 1
    This worked for me. Thanks Commented Aug 7, 2018 at 16:33
  • No problem. I would recommend reading that whole thread though, because depending on your build environment it can have effects on other parts of your app. I would do a thorough test of everything before moving forward to make sure it's not interfering with anything else Commented Aug 7, 2018 at 16:35

3 Answers 3

2

For typescript:

import { IpcRenderer } from 'electron';

declare global {
  interface Window {
    require: (module: 'electron') => {
      ipcRenderer: IpcRenderer
    };
  }
}

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

See https://github.com/electron/electron/issues/9920

Sign up to request clarification or add additional context in comments.

Comments

1

Encountered the same problem using react-create-app.

The fix, using window.require on the React side instead of import

const electron = window.require("electron")

and on the Electron side, when creating BrowserWindow add nodeIntegration: true as follow

mainWindow = new BrowserWindow({
    width: 900,
    height: 680,
    webPreferences: { nodeIntegration: true }
});

Comments

-1

The answers above are not current.

Here is the correct answer:

Since Electron v12, setting nodeIntegration: true has no affect. Instead, you need to set contextIsolation: false:

mainWindow = new BrowserWindow({
    // ...
    webPreferences: {
        contextIsolation: false,
    }
})

https://github.com/electron/electron/issues/7300#issuecomment-799093768

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.