在电子包装应用程序中构建时放置Env File

发布于 2025-02-08 16:19:48 字数 922 浏览 1 评论 0原文

我需要在构建时将一个.env文件放入包装应用中,然后将变量从.env将变量附加到process.env。我将.env放在与package.json.json的目录中,当我从NPM启动电子时,它起作用。当我构建MacOS应用程序并启动它时,它将无法使用(似乎.env文件已丢失)。

我的main.js启动了Java后端,因此我提供环境变量:

// This works in npm start, but not for packed app
process.env.MY_VAR = dotenv.config().parsed.MY_VAR;

this.serverProcess = require("child_process").spawn(
    "/usr/bin/env",
    ["sh", dirname + "/server/bin/embedded"],
    { env: process.env });

我的情况是:

  • 生成.env并将其放入电子文件夹(它通过构建系统自动生成)
  • 构建电子和包装电子
electron-builder --mac --publish never
  • 启动MacOS应用程序
  • 包装应用程序应使用提供的环境运行Java(来自.env

是否有一个示例或最佳实践,如何在包装构建时放置环境变量?

I need to put a .env file into a packed app while building, then attach variables from the .env to process.env. I put the .env in the same directory as the package.json and it works when I start electron from npm. It won't work when I build a MacOS app and start it (it seems as though the .env file is lost).

My main.js starts a java backend, so I provide environment variables:

// This works in npm start, but not for packed app
process.env.MY_VAR = dotenv.config().parsed.MY_VAR;

this.serverProcess = require("child_process").spawn(
    "/usr/bin/env",
    ["sh", dirname + "/server/bin/embedded"],
    { env: process.env });

My case is:

  • Generate .env and put it to electron folder (it generates automatically by build system)
  • Build electron and pack electron
electron-builder --mac --publish never
  • Start MacOS App
  • Packed app should run java with the provided environment (from .env)

Is there an example or best practices how to put environmental variables while package building?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2025-02-15 16:19:48

dotenvexpand做到了。

const dotenvExpand = require("dotenv-expand");

if (process.resourcesPath) {
    dotenvExpand.expand(dotenv.config({ path: path.join(process.resourcesPath, ".env") }));
}

dotenvExpand did the trick.

const dotenvExpand = require("dotenv-expand");

if (process.resourcesPath) {
    dotenvExpand.expand(dotenv.config({ path: path.join(process.resourcesPath, ".env") }));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文