Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.existsSync is not a function node.js/electron app

i am getting the error from a dependency (electron-edge) of an electron/node js app. The node.js version is 5.5.0 and electron version is 0.36.7

like image 456
user3651902 Avatar asked Feb 28 '16 10:02

user3651902


2 Answers

To complete Mark Meyer answer : if you're using create-react-app with electron, you cannot modify webpack without ejecting create-react-app. Here is a solution using @craco/craco :

npm i --save @craco/craco

Replace create-react-app by craco in your package.json scripts :

  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "electron": "electron ."
  },

Add craco.config.js to the root directory

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

Modify your main.js file:

mainWindow = new BrowserWindow(
  {
      width: 800,
      height: 600,
      webPreferences: {        // add
        nodeIntegration: true  // these
      }                        // lines
  });

Then from you js file, to open file dialog for example, use window.require :

const remote = window.require('electron').remote;
remote.dialog.showOpenDialog(remote.getCurrentWindow(), {properties:["openDirectory"]});

See create-react-app issue for the full story

like image 143
Benjamin Caure Avatar answered Nov 15 '22 21:11

Benjamin Caure


I guess you are using a bundler for your code. In this case, the problem arises, because you don't bundle with the correct target set.

In webpack include target: 'electron-renderer' into your webpack.config.js.

In parcel bundle with the correct target via the command line option --target electron (parcel build yourfile.html --target electron).

like image 44
Peter Lehnhardt Avatar answered Nov 15 '22 21:11

Peter Lehnhardt