electron-builder, how to set node environmental variables

Finn picture Finn · Jan 16, 2019 · Viewed 8.9k times · Source

Node.js in windows system can be set environmental before the server is started, like this:

set NODE_ENV=production 

That NODE_ENV parameter can be using in node.js or electron by process.env.NODE_ENV.

But when I builder electron by electron-builder, like this:

electron-builder build --windows

How do I set the environmental variables?


Update:

May be cannot pass a fixed environment variable to an executable by electron-builder.

Maybe you can only manually load an environment file, modify it when you package it, or preset the parameters to the dev state. When there is no state, it is production.

Answer

SigmaSoldier picture SigmaSoldier · Jan 16, 2019

If you want an environment variable to be set on runtime you can either set them manually or use other tools like dotenv https://www.npmjs.com/package/dotenv

But the easiest way is to set them at runtime when running the binaries. You can use either a batch script (If windows) for example:

setlocal
set NODE_ENV=production
.\your-binaries.exe
endlocal

Note: setlocal prevents the variable leaking any further.

The one-liner version could be set NODE_ENV=production && .\binaries.exe

Under linux works the same way: set variable then run.