1

** Edit, I believe this is an issue with the initial boilerplate I started with (one using parcel) and is missing some part causing this issue. This seems to work just fine with electron-quick-start boilerplate **

I have searched around and can't find a solution. There is a youtube video tutorial showing how to read from the file system: https://www.youtube.com/watch?time_continue=292&v=PQZEymiWFh8

But I implement the same but get this error fs.readFile is not a function. I am doing this in a react electron app however. Here is how I am setting this up:

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

...

getFile() {
    dialog.showOpenDialog((fileNames) => {
        // fileNames is an array that contains all the selected
        if(fileNames === undefined){
            console.log("No file selected");
            return;
        }

        fs.readFile(fileNames[0], 'utf-8', (err, data) => {
            if(err){
                alert("An error ocurred reading the file :" + err.message);
                return;
            }

            // Change how to handle the file content
            console.log("The file content is : " + data);
        });
    });
}

It gets the file name, but fs.readFile is throwing the not a function error. Anyone know a solution to this as it works in that tutorial video?

Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
Chipe
  • 4,641
  • 10
  • 36
  • 64
  • Possible duplicate of [Electron function to read a local file - FS - Not reading](https://stackoverflow.com/questions/43722450/electron-function-to-read-a-local-file-fs-not-reading) – Jared Smith Sep 10 '18 at 17:01
  • That shows how to do it, which is what I already am doing. Cant see anyone else asking about this error, trying to see if anyone else has come into this issue – Chipe Sep 10 '18 at 17:08
  • Works fine on my box. Steps I took: 1) clone electron-quick-start 2) edited renderer.js to require fs and `fs.readFile` itself and log it's own source to the console 3) npm install 4) npm start 5) checked the console and sure enough there it is. – Jared Smith Sep 10 '18 at 17:26
  • hmm, maybe the boiler plate I started with. I am using a parcel boilerplate I found online – Chipe Sep 10 '18 at 18:00
  • I dont have a renderer.js in this build. I will walk through it and see the differences between my build and the electron quick start and go from there. Thanks – Chipe Sep 10 '18 at 18:04
  • I would (and this almost always a good idea) strip it to a minimal test case, comment out everything but what is required to read a file in the current directory (like itself if applicable) and logs the contents/error to the console. Or you may just want to start over with that app skeleton I mentioned. – Jared Smith Sep 10 '18 at 18:04
  • No problem. If you edit your question (can be trivial) I'll remove the downvote. – Jared Smith Sep 10 '18 at 18:04
  • @JaredSmith Thanks jared. I added a note at the top – Chipe Sep 10 '18 at 20:10
  • It's seem like you're trying to read file in renderer process? – Hai Pham Sep 11 '18 at 02:07
  • what's `window.require`? If you attached require there (dont know why though), then you should use it in case of `fs` too. Also, a more clean approach is sending ipc message to main and do this job there – pergy Sep 13 '18 at 07:32

0 Answers0