electron 5.0.0 “Uncaught ReferenceError: require is not defined”

前端 未结 6 936
难免孤独
难免孤独 2020-12-24 06:06

I had initially been using electron stable (4.x.x), and was able to use require in both my browser and renderer processes. I upgraded to electron beta (5.0.0) b

6条回答
  •  难免孤独
    2020-12-24 06:37

    Readers of this post should read the Do not enable Node.js Integration for Remote Content section from the Security, Native Capabilities, and Your Responsibility Guide before making a decision.

    // Bad
    const mainWindow = new BrowserWindow({
      webPreferences: {
        nodeIntegration: true,
        nodeIntegrationInWorker: true
      }
    })
    mainWindow.loadURL('https://example.com')
    
    // Good
    const mainWindow = new BrowserWindow({
      webPreferences: {
        preload: path.join(app.getAppPath(), 'preload.js')
      }
    })
    mainWindow.loadURL('https://example.com')
    

提交回复
热议问题