0

I am using electron v1.2.3 with react js and es6 (with babel-preset-es2015).
I have a module where I need to import the app module in the render process. But I got this error:

Cannot read property 'app' of undefined

Here is the module that I need to import:

const app = require('electron').remote.app;
const userData = app.getPath('userData');

var nconf = require('nconf').file({file: userData + '/settings.json'});


function saveSettings(settingKey, settingValue) {
    nconf.set(settingKey, settingValue);
    nconf.save();
}
function readSettings(settingKey) {
    nconf.load();
    return nconf.get(settingKey);
}

module.exports = {
    saveSettings: saveSettings,
    readSettings: readSettings
};

I also have tried with

const app = require('electron').remote.require('app');

Still i get the error:

Cannot read property 'require' of undefined

Not sure is the webpack configuration matters. I am refer to this answer to setting up the webpack configuration.

module.exports = {
  context: __dirname + '/app',
  entry: './entry.js',
  node: {
    fs: "empty"
  },
  output: {
    filename: 'bundle.js',
    path: __dirname + '/build',
    publicPath: 'http://localhost:8080/build/'
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'react-hot',
    },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/ ,
        query: { presets:['es2015','react'] }
      }
    ]
  },
  externals: [
    (function () {
      var IGNORES = [
        'electron'
      ];
      return function (context, request, callback) {
        if (IGNORES.indexOf(request) >= 0) {
          return callback(null, "require('" + request + "')");
        }
        return callback();
      };
    })()
  ]
};
Community
  • 1
  • 1
dev-jim
  • 2,404
  • 6
  • 35
  • 61
  • I don't really understand what you're trying to do here? You're trying to import the app to load a json file? Can't you just use the absolute path to the json file? – erichardson30 Jun 29 '16 at 18:52
  • Actually I want to load the settings file, where saved in the userData path. And used it across all the render process. Or you have any recommendation to get the userData without using the app? – dev-jim Jun 30 '16 at 07:02
  • You could use some element of the [data storage APIs](http://electron.atom.io/docs/all/#how-to-share-data-between-web-pages) useable in Electron. LocalStorage seems to suit your needs. – Jens Habegger Jul 04 '16 at 14:07
  • Yes, you are right. Uisng those module is more easier. – dev-jim Jul 04 '16 at 17:49

0 Answers0