Gernot Raudner
Gernot Raudner

Reputation: 824

Why can't my electron app access the 'fs' module?

I have a relatively simple electron app - brunch, react, redux, electron. I'm now trying to read a json file using the "fs" node module, but it's throwing an error:

TypeError: _fs2.default.readFile is not a function

After investigating it looks like the module doesn't contain anything. Compilation says it's available and bundled, but the resulting vendor.js looks weird. Here's the probably relevant part:

[...]
var global = typeof window === 'undefined' ? this : window;require.register("fs", function(exports, require, module) {
  module.exports = {};
});
[...]

It doesn't look like brunch correctly bundles these native modules. I also tried it with the electron module and ipc but there it already fails upon loading electron/index.js when trying to require('path').

Here's my brunch-config.js:

exports.files = {
  javascripts: {
    joinTo: {
      'js/vendor.js': /^(?!app)/,
      'js/app.js': /^app/
    }
  },
  stylesheets: {joinTo: 'app.css'}
};

exports.plugins = {
  babel: {
    presets: [
      ['env',
        {
          targets: {
            "browsers": ["Electron >= 1.7.8"]
          }
        }
      ],
      'stage-3',
      'react'
    ]
  }
};

Did I forget anything in my brunch config? I'm using ES6 imports, but the problem also happens if I use commonjs.

Upvotes: 3

Views: 655

Answers (0)

Related Questions