【问题标题】:Process is not defined for Electron's Getting Started App [duplicate]未为 Electron 的入门应用程序定义流程 [重复]
【发布时间】:2021-06-11 05:08:42
【问题描述】:

我正在尝试开始使用 Electron。我已经能够运行所有simple examples。它们都按预期工作。当我尝试关注Quick Start Guide 时,我遇到了与this question 中提到的相同的问题:应用程序正常启动,但不显示节点 Chrome 和 Electron 的版本。当我查看开发工具时,我看到了这个错误:

Uncaught ReferenceError: process is not defined
    at index.html:14

但是,我已经nodeIntegration 设置为true

为什么还是不行?

版本:

  • npm 7.6.3
  • 节点 v14.16.0
  • 铬 89.0.4389.82

操作系统:

  • Ubuntu 20.04.2 LTS。

index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>

<body style="background: white;">
    <h1>Hello World!</h1>
    <p>
        We are using node
        <script>document.write(process.versions.node)</script>,
        Chrome
        <script>document.write(process.versions.chrome)</script>,
        and Electron
        <script>document.write(process.versions.electron)</script>.
    </p>
</body>

</html>

ma​​in.js

const { app, BrowserWindow } = require('electron')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

package.json

{
  "name": "my-electron-app",
  "version": "0.1.0",
  "author": "username",
  "description": "My Electron app",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron": "^12.0.0"
  }
}

【问题讨论】:

    标签: node.js linux npm electron chromium


    【解决方案1】:

    在创建BrowserWindow 时尝试设置此值:

    webPreferences: { nodeIntegration: true, contextIsolation: false }
    

    一个新的major Electron version has been released 破坏了教程。
    具体的重大更改是contextIsolation 标志的新默认值。

    更多详情请见this GitHub issue

    【讨论】:

    • 是否有禁用nodeIntegration 的替代方案?我正在升级旧的 Electron 3 应用程序,但我不知道哪种方法更受欢迎,是否需要稍微重构我的应用程序。
    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2017-12-08
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多