簡體   English   中英

無法在我的 preload.js 中使用模塊“dotenv”,即使我的依賴項中有它

[英]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: falsenodeIntegration: true

例子

app.whenReady().then(() => {
  const win = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true
    }
  })
  win.loadURL('https://google.com')
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM