繁体   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