[英]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.