简体   繁体   English

类型错误:window.require 不是 function

[英]TypeError: window.require is not a function

Im trying to build an electron app and want to use window.require.我正在尝试构建一个 electron 应用程序并想使用 window.require。 Unfortunately the compiler says "TypeError: window.require is not a function".不幸的是,编译器说“TypeError:window.require 不是函数”。 Ironically require works only in main.js.具有讽刺意味的是,要求在 main.js 中有效。

Here the code Im trying to run:这是我试图运行的代码:

const electron = window.require('electron')
const low =  window.require('lowdb')
const FileSync = window.require('lowdb/adapters/FileSync')

I read in another post that somebody have had the same problem and it was fixed by adding this code into the.html file:我在另一篇文章中读到有人遇到了同样的问题,并通过将此代码添加到 .html 文件中来解决此问题:

    <script type="text/javascript" src="../../../Gehaltseinstellungen_Hinzufügen.js">
        window.nodeRequire = require;
        delete window.require;
        delete window.exports;
        delete window.module;
    </script>

Also the author said using "nodeRequire" instead of require would solve the problem but it doesn't...作者还说使用“nodeRequire”而不是require可以解决问题,但它并没有......

Another option I read about is that the NodeIntegration is set to false while the rendering process is activated, but I don't know how to activate Node while rendering.我读到的另一个选项是在激活渲染过程时将 NodeIntegration 设置为 false,但我不知道如何在渲染时激活 Node。

It is unclear what version of Electron you are using.目前尚不清楚您使用的是哪个版本的Electron The syntax you are using is non-standard.您使用的语法是非标准的。

First – if you are using Electron 5.0, nodeIntegration is false by default in BrowserWindows so you need to specify it explicitly when you create your window:首先——如果您使用的是Electron 5.0,则BrowserWindows中的BrowserWindows 默认为 false,因此您需要在创建窗口时明确指定它:

mainWindow = new BrowserWindow({
  width: 800,
  height: 600,
  webPreferences: {
    nodeIntegration: true
  }
})

Given the above, the syntax below works fine (ie no 'window' reference needed):鉴于上述情况,下面的语法工作正常(即不需要“窗口”引用):

const { ipcRenderer, remote } = require('electron');

you can set the webPreferences.contextIsolation to be false like this您可以像这样将webPreferences.contextIsolation设置为false

    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
    }

it maybe works它可能有效

Needed all 3 setup like so to make this work:需要像这样的所有 3 个设置才能完成这项工作:

webPreferences: {
  nodeIntegration: true,
  enableRemoteModule: true,
  contextIsolation: false,
},

note: macbook m1, big sur 11.4, maybe it has to do something about OS, idk.注意:macbook m1,big sur 11.4,也许它必须对操作系统,idk 做一些事情。

Same issue in Electron + React + Typescript. Electron + React + Typescript 中的相同问题。 This solved it for me这为我解决了

webPreferences: {
    nodeIntegration: true,
    enableRemoteModule: true,
    contextIsolation: false
}

I also met this issue in Electron + Angular.我也在 E​​lectron + Angular 中遇到过这个问题。

  webPreferences: {
    nodeIntegration: true,
    contextIsolation: false
  }

Configuration above works for me.上面的配置对我有用。

I was stuck with this problem for a couple of days.我被这个问题困扰了几天。 Could not figure out for the life of me.无法弄清楚我的生活。 Went through a lot of docs and stackoverflow and finally!!!!浏览了很多文档和stackoverflow,最后!!!!

I fixed this error the following way :我通过以下方式修复了此错误:

create a file preload.js :创建一个文件 preload.js :

    const { remote } = require('electron');
    window.ipcRenderer = require('electron').ipcRenderer;

then in main.js/electron.js:然后在 main.js/electron.js 中:

  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      //this line is crucial
      preload: path.join(app.getAppPath(), '/path-to-file/preload.js'),
      contextIsolation: false
    }
  })

and finally in App.js:最后在 App.js 中:

const { ipcRenderer } = window

I got ipcRenderer in window from electron.我从电子窗口中获得了 ipcRenderer。 I'm pretty sure you can get anything else that you would want.我很确定你可以得到任何你想要的东西。

You don't need to modify web preferences as has been suggested, and I would recommend not doing it unless you have a better reason to because of the implied security issues ( https://www.electronjs.org/docs/latest/tutorial/security#isolation-for-untrusted-content ).您不需要按照建议修改 web 首选项,我建议您不要这样做,除非您有更好的理由,因为隐含的安全问题( https://www.electronjs.org/docs/latest/tutorial /security#isolation-for-untrusted-content )。

Instead, what you can do is use the preload script to add any functionality you need to window .相反,您可以做的是使用预加载脚本将您需要的任何功能添加到window Like this for example:像这样的例子:

preload.js (located in the root folder): preload.js(位于根文件夹中):

contextBridge.exposeInMainWorld('ipcRenderer', {
  invoke: (event) => ipcRenderer.invoke(event),
});

webPreferences in main.js: main.js 中的 webPreferences:

webPreferences: {
  preload: path.join(__dirname, 'preload.js'),
}

Now in your code you can reference it:现在在您的代码中,您可以引用它:

const { ipcRenderer } = window as any;
const response = await ipcRenderer.invoke(...);

Reference documentation here: https://www.electronjs.org/docs/latest/tutorial/tutorial-preload#communicating-between-processes此处参考文档: https://www.electronjs.org/docs/latest/tutorial/tutorial-preload#communicating-between-processes

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

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