简体   繁体   English

Electron - 要求未定义,尽管 nodeIntegration 为真

[英]Electron - require is not defined, despite nodeIntegration being true

my problem is - I got a main.js file, and a functions.js file, where I want some of my functions to be.我的问题是 - 我有一个 main.js 文件和一个 functions.js 文件,我希望我的一些函数在其中。 However, whenever I use require in that functions.js file, I get an error, require is not defined.但是,每当我在该 functions.js 文件中使用 require 时,都会出现错误,require 未定义。

I've read posts about other people having similar problem, but in their case setting nodeIntegration to true helped.我读过关于其他人有类似问题的帖子,但在他们的情况下,将 nodeIntegration 设置为 true 有帮助。 That's what I had from the beginning.这就是我从一开始就拥有的。 I know the problems of this solution, but at the moment I don't need the app to be secure, so I would be fine with this solution, if it worked.我知道这个解决方案的问题,但目前我不需要应用程序是安全的,所以如果它有效,我会很好地使用这个解决方案。 It doesn't.它没有。

I tried preloading, but I think it's just a "safer" equivalent of nodeIntegration solution.我尝试过预加载,但我认为它只是 nodeIntegration 解决方案的“更安全”等价物。 And being an equivalent, it doesn't work either.作为一个等价物,它也不起作用。 Can you help me?你能帮助我吗?

My code:我的代码:

main.js main.js

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

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: true,
      preload: 'functions.js'
    }
  })
  win.maximize();
  win.loadFile('index.html');
}

app.on('ready', function() {
  createWindow();
  createOpenDialog();
});

functions.js (literally, this is all it takes for the code to fail) functions.js (从字面上看,这就是代码失败所需要的全部)

const electron = require('electron');

index.html索引.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="main.css">
    <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;">
    <script src="functions.js">
    </script>
</body>
</html>

So, one thing I didn't try was setting contextIsolation to false .所以,我没有尝试的一件事是将 contextIsolation 设置为 false And least it seems like it, because I was sure I tried it.至少看起来是这样,因为我确定我试过了。 Anyway, this seems to fix the issue, although I will need to learn what it does exactly.无论如何,这似乎解决了这个问题,尽管我需要了解它究竟做了什么。 I can do "require" now, and already tested if it works by using ipcRenderer.我现在可以做“要求”,并且已经通过使用 ipcRenderer 测试了它是否有效。

Context isolation is a security feature that exists on Electron.上下文隔离是 Electron 上存在的安全功能。 Its whole purpose is to separate the preload scripts and Electrons internal apis from your website so it doesn't have any access it should not have.它的全部目的是将预加载脚本和 Electrons 内部 api 从您的网站中分离出来,因此它没有任何不应该拥有的访问权限。

From Electron 12 context isolation is off by default.This means that if you need to expose some functionality in your preload scripts you will have to use context bridge从 Electron 开始 12 上下文隔离默认关闭。这意味着如果您需要在预加载脚本中公开某些功能,则必须使用上下文桥

An example from the documentation is this:文档中的一个示例是这样的:

Before context isolation was off在上下文隔离关闭之前

window.myAPI = {
  doAThing: () => {}
}

After: context isolation is on之后:上下文隔离开启

const { contextBridge } = require('electron')

contextBridge.exposeInMainWorld('myAPI', {
  doAThing: () => {}
})

You can read more about this feature in the docs: https://www.electronjs.org/docs/tutorial/context-isolation您可以在文档中阅读有关此功能的更多信息: https://www.electronjs.org/docs/tutorial/context-isolation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM