問題描述
我正在嘗試構建一個電子應用程序并想使用 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,BrowserWindows
中的 nodeIntegration 默認為 false,因此您需要在創建窗口時明確指定它:
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');
這篇關于類型錯誤:window.require 不是函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!