出现问题:
正确安装electron之后,复制了官方代码:
const { app, BrowserWindow } = require('electron')
console.log(electron)
function createWindow () {
// 创建浏览器窗口
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// 加载index.html文件
win.loadFile('index.html')
}
app.on('ready', createWindow)
发现会报错:
TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (D:\spider\e+p.js:22:5)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11
谷歌了一下:
原来问题在于我用的是node electronTest.js启动的,实际上是需要在package.json 里的scripts字段里增加:
{
"scripts": {
"start": "electron ./electronTest"
}
}