[英]Cannot use module "dotenv" in my preload.js even if I have it in my dependencies
我剛開始使用 Electron。
這是我的preload.js
的開始:
const { contextBridge } = require('electron');
require('dotenv').config();
// ...
當我使用npm start
,應用程序正常啟動,除了 preload.js 沒有做任何事情。 我打開開發者工具,看到這個錯誤:
Error: module not found: dotenv
at preloadRequire (...)
...
然后我檢查了我的npm-shrinkwrap.json
:
"devDependencies": {
// ...
"dotenv": "^16.0.3",
"electron": "^22.1.0"
}
好吧,它確實有dotenv
。
那么,如何讓preload.js
能夠使用dotenv
呢?
感謝Alexander Leithner ,我解決了這個問題。
在文檔中,它說“沙箱”限制了我可以從 preload.js 中“要求”的內容; 所以要禁用它,請在BrowserWindow
選項的webPreferences
中設置sandbox: false
或nodeIntegration: true
。
app.whenReady().then(() => {
const win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})
win.loadURL('https://google.com')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.