我使用电子v1.2.3与反应js和es6(与babel-preset-es2015)。
我有一个模块,我需要在渲染过程中导入应用程序模块。但我得到了这个错误:
无法读取未定义的属性'app'
以下是我需要导入的模块:
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
};
我也试过
const app = require('electron').remote.require('app');
我仍然得到错误:
无法读取未定义的属性'require'
不确定webpack配置是否重要。我引用此answer来设置webpack配置。
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();
};
})()
]
};