TypeError:window.require不是一个函数 [英] TypeError: window.require is not a function
问题描述
我正在尝试构建一个电子应用程序,并希望使用window.require。不幸的是,编译器说 TypeError:window.require不是一个函数。具有讽刺意味的是, require 仅在main.js中有效。
Im trying to build an electron app and want to use window.require. Unfortunately the compiler says "TypeError: window.require is not a function". Ironically require works only in 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')
我在另一篇文章中读到有人遇到了同样的问题,可以通过将以下代码添加到.html文件中来解决此问题:
I read in another post that somebody have had the same problem and it was fixed by adding this code into the .html file:
<script type="text/javascript" src="../../../Gehaltseinstellungen_Hinzufügen.js">
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>
另外,作者说使用 nodeRequire代替require可以解决问题,但不能解决。 ..
Also the author said using "nodeRequire" instead of require would solve the problem but it doesn't...
我读到的另一个选项是激活渲染过程时NodeIntegration设置为false,但是我不知道如何在渲染时激活Node。
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.
推荐答案
目前尚不清楚您使用的 Electron
是哪个版本。
It is unclear what version of Electron
you are using. The syntax you are using is non-standard.
首先-如果使用的是 Electron
5.0,则 nodeIntegration在 BrowserWindows $ c $中默认为假
c>,因此您需要在创建窗口时明确指定它:
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:
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
鉴于上述情况,以下语法可以正常工作(即无需窗口引用):
Given the above, the syntax below works fine (i.e. no 'window' reference needed):
const { ipcRenderer, remote } = require('electron');
这篇关于TypeError:window.require不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!