fs.existsSync is not a function

15 min read

Describe the bug
I'm getting fs.existsSync is not a function when I'm importing electron in my component.

Possible solution
This error is most likely occurring because the fs module is not available in the renderer process in Electron, and therefore fs.existsSync cannot be used in the component.

One solution is to use the ipcRenderer module to send a message to the main process, where the fs module is available, and have the main process check for the existence of the file and return a result to the renderer process.

Alternatively, you can use the fs-extra module, which provides additional functionality to the fs module and also works in the renderer process.

To use fs-extra in your component, you can install it using npm:

npm install fs-extra

Then, you can import it in your component:

const fs = require('fs-extra');

and use its equivalent function to check for the existence of a file:

if (await fs.pathExists('/path/to/file')) {
  // file exists
} else {
  // file does not exist
}

or synchronously:

if (fs.existsSync('/path/to/file')) {
  // file exists
} else {
  // file does not exist
}