【问题标题】:nodeIntegration true ignored?nodeIntegration true 被忽略了吗?
【发布时间】:2020-04-18 18:39:46
【问题描述】:

我正在尝试在附加到 html 文件的 js 文件中使用 require 指令。但是,我收到与该文件有关的未定义错误消息。我的理解是在 5.0 之后的较新版本的 Electron 中默认禁用 nodeIntegration。但是,即使在 web 首选项中启用 nodeIntegration 后,我仍然会收到 require 错误消息。我的理解是这个 nodeIntegration 应该解决这个问题。为什么我仍然遇到 require 未定义的问题?这是 main.js 文件的相关部分。

编辑:我在 preload 和 nodeIntegration 之间缺少一个逗号,因此感谢指出这一点的人!但是,我仍然遇到这个问题。仍然收到“Uncaught ReferenceError: require is not defined”。

第二次编辑:这是该问题的最小再现。即使将 nodeIntegration 设置为覆盖它,require 函数也是未定义的。最后,我只是尝试从本地 json 文件中读取,但是在使用电子时,我可以找到的每个实例都以简单的方式执行此操作,以某种方式使用 require,无论是需要 fs 还是需要文件。如果 require 语句根本不起作用,我不能使任何一个工作。

main.js:

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration:true
    }
  })
  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) createWindow()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
    <link href="main.css" rel="stylesheet">
    <title>Destiny Guide</title>
   <script src="problem.js"></script>
  </head>
  <body>
    <h1>Hello World</h1>
    Hello World
    <br>
    <h1><a href = "HelloWorld">helloWorld</a><h1>

    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>

问题.js:

var dataArc = require("./ZeroHourArc.json");

第三,也是最后,编辑:见下面的答案。

【问题讨论】:

  • preload: path.join(__dirname, 'preload.js')nodeIntegration:true 之间是否缺少逗号?
  • @snwflk 我错过了那个逗号,谢谢。但是,由于某种原因,我仍然遇到此问题。
  • 您能否发布一个完整但最小的示例来重现该问题?错误信息显示在哪里?浏览器窗口是否显示?
  • 顺便说一句,无论nodeIntegration 是打开还是关闭,您都可以在预加载脚本中使用require(我已经尝试过了,它工作正常)。要引用有关预加载脚本的文档:This script will always have access to node APIs no matter whether node integration is turned on or off.
  • @snwflk 我用最小版本的代码更新了它。

标签: electron


【解决方案1】:

我有点忘记了。我正在编辑一个与我的 main.js 具有相同名称和布局的随机文件,但它不在同一个地方。在正确的位置编辑正确的文件,同名,让我可以让 nodeIntegration 按预期工作。

【讨论】:

    【解决方案2】:

    试着像这样添加contextIsolation: false

    mainWindow = new BrowserWindow({
      width: 500,
      height: 500,
      icon: './public/logo.png',
      backgroundColor: 'white',
      webPreferences: {nodeIntegration: true, contextIsolation: false}
    });
    

    【讨论】:

      【解决方案3】:

      在 Electron 12 中,contextIsolation 将默认启用,因此除非nodeIntegration = truecontextIsolation = false,否则require() 不能在渲染器进程中使用。

      更多详情见:https://github.com/electron/electron/issues/23506

      【讨论】:

        猜你喜欢
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 2011-12-18
        • 2019-03-07
        • 2020-06-18
        • 2012-02-17
        • 1970-01-01
        相关资源
        最近更新 更多