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?
.env
file access is different than for the application.env
file access. So if I just include a.env
file in the root of my project next to thepackage.json
then it will get picked up in the application. However this still doesn't work formain.js
. – Deservedly