首页 > 解决方案 > nodeIntegration true 被忽略了吗?

问题描述

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

编辑:我在 preload 和 nodeIntegration 之间缺少一个逗号,所以感谢指出这一点的人!但是,我仍然遇到这个问题。仍然收到“未捕获的 ReferenceError:未定义要求”。

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

主.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.

索引.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");

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

标签: electron

解决方案


只需尝试contextIsolation: false像这样添加:

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

推荐阅读