3

I'm trying to write a little proof of concept app with React and Electron, following the tutorial here:

https://medium.com/@brockhoff/using-electron-with-react-the-basics-e93f9761f86f

I have one button called "Save me!" in the App component. I wrote the handler to print out "Hello World" to console, which worked, so I know the handler is not the problem. In memory, I have a simple object:

var json = {
    "foo":"bar",
    "bar":"none"
}

In the handler is a typical function to write the contents of json to a file:

var fs = require('fs');
fs.writeFile("temp.txt", json, (err) => {
    if (err) console.log(err);
    console.log("Successfully Written to File.");
});

However, when I press on the button, the console spits out an error message stating that fs.writeFile is not a function. Any help here?

Edit: Why is this question marked as duplicate, when the a post in the "duplicate" question literally says:

Also take a look at https://electronjs.org/ It allows you to use nodejs APIs like fs in you "native"-like react app.

How is this a duplicate question when the addition of Electron clearly seems to be a significant difference between the two?

Edit 2: For clarification, fs does work on Electron. I had to invoke it in a somewhat weird way:

var app = window.require('electron').remote;
const fs = app.require('fs');

For anyone who is having trouble with window.require, use rm -rf node_modules && npm install to reinstall your node modules

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

Problem solved.

erli
  • 358
  • 4
  • 16

0 Answers0