Electron出現 Cannot read property 'on' of undefined報錯

出現問題:
正確安裝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"
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章