您可以在电子生产中使用dotenv吗?
我正在构建一个电子,React应用程序,其中我需要一个 dotenv
文件。
在代码中,我使用 process.env.variable
调用我的变量,该变量在开发中正常。
但是,一旦用电子构建器
分发, dotenv变量
不再工作( undefined
)。
我现在的问题是,甚至可以这样做,如果是,如何?我试图在互联网上找到解决方案,但似乎人们并没有真正面临这个问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这个问题是否仍然有效,但这是我得到的,以防有人有同样的问题。如果您使用的是
电子构建器
,目前尚不在本地支持。,
但这并不意味着没有其他方法,这是我对此的肮脏方式,我创建了一个脚本来替换用于
电子构建器构建-Publish 。
我正在使用React样板,在您受尊敬的框架上可能有所不同。因此,我创建一个脚本来替换
app/dist/main/main.js
在WebPack完成将我的TS转换为JS文件时,它应该就在Eletron-Builder << /code> bash行被执行。
这是我所做的事情的详细信息:
文件夹结构
package.json
:package> package> packageenv.js
的代码(超级肮脏,我建议阅读和重构它)。也放置了一些todo
。Not sure if this question is still valid, but here is what I got in case anyone has the same question. If you are using
electron-builder
, this is not supported natively at the moment.https://github.com/electron-userland/electron-builder/issues/7143
But that does not mean there aren't any other way around, here is my dirty way of work around on this, I created a script to replace the env placeholder used for
electron-builder build --publish
.I'm using React boilerplate, it might be different on yours respected framework. So I create a script to replace env in the
app/dist/main/main.js
when the webpack finished transpiling my TS to JS files, it should be right before theeletron-builder
bash line got executed.Here is a detailed of what I did:
Folder structure
package.json
:Code for
packageEnv.js
(it's super dirty, I would recommend to read and refactor it). Placed someTODO
there as well.