electron icon indicating copy to clipboard operation
electron copied to clipboard

Error while importing electron in react | import { ipcRenderer } from 'electron'

Open Amthieu opened this issue 9 years ago • 88 comments

I have created a simple react app with create-react-app and I have integrated it with electron successfully. Everything was working great until I tried to import electron inside the action creator file. If I remove the line below, the app works fine. The problem is that I can't use the ipcRenderer to communicate from the react side to the electron main process.

This line causes the app to crash: import { ipcRenderer } from 'electron';

I get the following error:

TypeError: fs.existsSync is not a function (anonymous function) node_modules/electron/index.js:6

  3 | 
  4 | var pathFile = path.join(__dirname, 'path.txt')
  5 | 
> 6 | if (fs.existsSync(pathFile)) {
  7 |   module.exports = path.join(__dirname, fs.readFileSync(pathFile, 'utf-8'))
  8 | } else {
  9 |   throw new Error('Electron failed to install correctly, please delete node_modules/electron and try installing again')

I found out on Google that this is a common problem when trying to import electron.

Thanks for the help

Amthieu avatar Jul 03 '17 19:07 Amthieu

CRA uses webpack which messes with standard module loading (including fs).

I'd recommend looking into the Electron mode for webpack and ejecting from CRA

MarshallOfSound avatar Jul 03 '17 21:07 MarshallOfSound

GitHub issues are for feature requests and bug reports, questions about using Electron should be directed to the community or to the Slack Channel.

MarshallOfSound avatar Jul 03 '17 21:07 MarshallOfSound

@MarshallOfSound my mistake.

I found the solution in issue #7300 if it can help anyone.

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

Please note that this will work when you run the Electron app, but if you just want to test your React code inside the browser it will still crash (window.require is not defined in the browser as it is in Electron).

Amthieu avatar Jul 08 '17 02:07 Amthieu

If you want to access app.quit(), you can use this:

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

Maybe it helps someone...

ciriousjoker avatar Jul 31 '17 07:07 ciriousjoker

@CiriousJoker these is solutions, thanks!

hendrixroa avatar Aug 16 '17 22:08 hendrixroa

I'm still getting window.require is not a function. I'm using Electron with React Starter Kit (https://github.com/kriasoft/react-starter-kit). Everything is working nicely, except this.

I've set my Electron app to load my app from the web, so the app is not running locally: https://gist.github.com/holgersindbaek/68f6db82f507967a51ca75c527faeff6

What I'm trying to do, is call the ipcRenderer in one of my React files. I'm not sure if it's even possible when my app is being loaded from the web though. Any suggestions?

holgersindbaek avatar Sep 20 '17 13:09 holgersindbaek

@holgersindbaek

In the same boat as you... Did you find a solution?

HemalR avatar Oct 14 '17 17:10 HemalR

No. I'm pretty sure it's not possible to load the ipcRenderer from the browser.

holgersindbaek avatar Oct 14 '17 17:10 holgersindbaek

If you are running your React app in the browser it won't work. Run it inside Electron and you should be fine.

Amthieu avatar Oct 14 '17 18:10 Amthieu

@Amthieu Thanks for the advice. I'm still in doubt as to how I can make my React project (based on React Starter Kit) run in Electron. Any advice would be greatly appreciated:

https://discuss.atom.io/t/getting-electron-to-work-with-react-starter-kit/48594

holgersindbaek avatar Oct 14 '17 18:10 holgersindbaek

Right, I have a solution.

  1. Create a preload.js file with the code:
window.ipcRenderer = require('electron').ipcRenderer;
  1. Preload this file in your main.js via webPreferences:
  mainWindow = new BrowserWindow({
    width: 800, 
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      preload: __dirname + '/preload.js'
    }
  });
  1. Now, you will have access from your react app. E.g. this will work:
componentDidMount() {
		if (isElectron()) {
			console.log(window.ipcRenderer);
			window.ipcRenderer.on('pong', (event, arg) => {
				this.setState({ipc: true})
			})
			window.ipcRenderer.send('ping')
		}
	}

Note - using this: https://github.com/cheton/is-electron for the isElectron() function

HemalR avatar Oct 16 '17 01:10 HemalR

@HemalR Step 3 should be the following (now):

componentDidMount() {
	if (window.isElectron) {
		console.log(window.ipcRenderer);
		window.ipcRenderer.on('pong', (event, arg) => {
			this.setState({ipc: true})
		})
		window.ipcRenderer.send('ping')
	}
}

Note: window.isElectron is not a function.

astrotars avatar Dec 01 '17 16:12 astrotars

@nparsons08

Apologies - should have added where I am getting isElectron from, have edited my code example with the link to: https://github.com/cheton/is-electron

HemalR avatar Dec 02 '17 04:12 HemalR

@holgersindbaek Is there a solution now

sandywk avatar Jun 04 '18 03:06 sandywk

for me only work if nodeIntegration is true;

webPreferences: {
      nodeIntegration: true, 
      preload: __dirname + '/preload.js'
}

carlosdelfino avatar Jul 02 '18 23:07 carlosdelfino

work great the @HemalR solution!

now HOW to send FROM electron TO React?

tried with on electron side

 ipcMain.emit("pong", "Hello!"); 

but nothing got received from the React listener

window.ipcRenderer.on("pong", (event, arg) => {
        console.log("PONG");
});

is correct to use ipcMain.emit() or should I use something else?

gino8080 avatar Sep 01 '18 15:09 gino8080

ok just found I have to use (on the electron main process)

mainWindow.webContents.send("pong", "Hello!");

thank you to all!

gino8080 avatar Sep 01 '18 15:09 gino8080

I tried all of the above to no avail. What worked for me was a giant hack. Modify the file ./node_modules/electron/index.js and hard code the path to electron.exe

e.g.

function getElectronPath() {
  return 'D:\\Repos\\MyProject\\node_modules\\electron\\dist\\electron.exe';
}

module.exports = getElectronPath();

chchmatt avatar Nov 04 '18 06:11 chchmatt

Wow, I couldn't get the IPCRenderer working on my React Components. I've tried all of the method above. Did any of you incidentally have any hints that I can use for it to be working? thanks

cyclonstep avatar Nov 15 '18 08:11 cyclonstep

Hmmm... My electron app still works just fine using my solution above - but I haven't updated it in a few months now (haven't needed to).

I wonder if there is a breaking change that would stop this from working? Maybe you guys can post your electron versions?

@cyclonstep Is there any specific error you were getting? Hard to help without a code snippet or some logs...

HemalR avatar Nov 19 '18 05:11 HemalR