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();
};
})()
]
};