question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using FS in CRA + Electron

See original GitHub issue

Is this a bug report?

Unsure.

I’m trying to use create-react-app + electron.js to make a cross-platform desktop app. In that app, I want to save and load the user’s files, like Word or Excel. To do so, I need believe I need to use node.js’s fs module, which I believe is somehow blocked in CRA, but I also need to use electron.js’s dialog as well. Requiring that module throws an error when electron itself can’t use fs.

I don’t know if this is an error in how I’m thinking about the app, an error in how I’ve configured my app, an error in react, create-react-app, or electron, or if it’s something else. Any help would be appreciated.

I have not ejected the app.

Did you try recovering your dependencies?

Yes. I rebuilt the project essentially from scratch here: https://github.com/arcandio/fstest2

Which terms did you search for in User Guide?

I’ve looked for

  • react fs empty
  • react electron fs
  • react use fs
  • create-react-app fs

Environment

System: OS: Windows 10 CPU: x64 AMD FX™-8350 Eight-Core Processor Binaries: Yarn: 1.15.2 - C:\Users\arcan\AppData\Roaming\npm\yarn.CMD npm: 5.6.0 - C:\Program Files (x86)\nodejs\npm.CMD Browsers: Edge: 44.17763.1.0 Internet Explorer: 11.0.17763.1 npmPackages: react: ^16.8.4 => 16.8.6 react-dom: ^16.8.4 => 16.8.6 react-scripts: 2.1.8 => 2.1.8 npmGlobalPackages: create-react-app: Not Found

Steps to Reproduce

(Write your steps here:)

  1. clone or download repo https://github.com/arcandio/fstest2
  2. run npm run electron-dev
  3. observe error inside electron when attempting to access electron’s dialog via the remote

Expected Behavior

Expected no error on require: https://electronjs.org/docs/api/dialog, as that documentation indicates that const { dialog } = require('electron').remote should be valid outside electron’s main process.

Actual Behavior

App throws an error:

TypeError: fs.existsSync is not a function
getElectronPath
F:/freelance/repos/creative-ontology-editor/node_modules/electron/index.js:8
   5 | var pathFile = path.join(__dirname, 'path.txt');
   6 | 
   7 | function getElectronPath() {
>  8 |   if (fs.existsSync(pathFile)) {
   9 |     var executablePath = fs.readFileSync(pathFile, 'utf-8');
  10 | 
  11 |     if (process.env.ELECTRON_OVERRIDE_DIST_PATH) {

image

Reproducible Demo

https://github.com/arcandio/fstest2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
agatcommented, May 13, 2019

@arcandio, @heyimalex I found solution without eject, with https://www.npmjs.com/package/@craco/craco.

craco.config.js

module.exports = {
    webpack: {
        configure: {
            target: 'electron-renderer'
        }
    }
};

And check that BrowserWindow has nodeIntegration: true property:

new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        nodeIntegration: true
    }
});
1reaction
heyimalexcommented, Apr 9, 2019

Pretty certain you’ll need to eject. The relevant line is here; we mock out fs because it’s not available in the browser. This probably will not change; create-react-app is built for running in a normal browser environment. But I feel your pain, as our electron app at work uses ejected cra 😂

Read more comments on GitHub >

github_iconTop Results From Across the Web

how can I use fs in react with electron? - Stack Overflow
I want use react+webpack+electron to build a desktop app.How can I inject fs module into react so that I can use it to...
Read more >
How do you enable fs module when using CRA + Electron
I have tried to setting nodeIntegration: true , I tried erasing fs: 'empty' from webpack.config.js in react-scripts . I also tried using window....
Read more >
Hello there. You can use electron modules in the renderer ...
Hello there. You can use electron modules in the renderer process by sending messages through the ipc module. Have you also tried the...
Read more >
ipcRenderer in CRA managed app - Dennis Tretyakov
The problem is — if you try to import ipcRender in web application, it crashes with TypeError: fs.existsSync is not a function ....
Read more >
Using NodeJS APIs in Electron with React - Ryosuke
As someone who has built Electron apps before, I thought I knew how to use NodeJS. I literally built an app that the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found