2

How do I set environment variables that will be accessible when the electron app is built and ran from the installer, not the cli? Using electron-builder V23.6 (latest)

Here's my build script in package.json:

"build:electron": "set MYVAR=true && electron-builder build --config electron-builder.config.js"

When I try to read process.env.MYVAR, I get undefined. The env variable is accessible in the builder config.js, so maybe there's an option I can relay from there?

LAZ
  • 402
  • 4
  • 15
  • You cannot set env vars during build and have them work as env vars when the application is running on a different machine. The [only working answer](https://stackoverflow.com/a/54215867/327074) I've seen so far is to use a batch/bash file to start the application and set the env vars there. [`dotenv-expand`](https://stackoverflow.com/a/76404441/327074) looks promising. I have tried [`electron-builder.env`](https://www.electron.build/configuration/configuration#environment-variables-from-file) - but that doesn't work for me so far. – icc97 Jun 21 '23 at 15:43
  • I've added an answer to the question I mentioned previously: https://stackoverflow.com/a/76535660/327074 using `dotenv` and electron-builders' `extraResources`. – icc97 Jun 22 '23 at 21:11

2 Answers2

0

Try this. Might work

build:electron": "MYVAR=true electron-builder build --config electron-builder.config.js"
0

You will need the cross-env package. After adding it with npm or yarn, change the given line to

"build:electron": "cross-env MYVAR='true' electron-builder build --config electron-builder.config.js"