electron-builder, how to set node environmental variables
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.
Finn
Updated on June 07, 2022Comments
-
Finn almost 2 years
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 innode.js
orelectron
byprocess.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 isproduction
. -
Finn over 5 yearsWhat does “CLI usage” mean? I created a
electron-builder.env
file in the current dir and inputNODE_ENV=production
, and builder electron, It's not work. I try to working inelectron .
to start and test showconsole.log(process.env.NODE_ENV)
, It's undefined. -
Sigma Octantis over 5 years@AlbertChen CLI ( en.wikipedia.org/wiki/Command-line_interface ) means that this file is read by the command
electron-builder
at runtime,electron
will not read it. -
Finn over 5 yearsI was try to build electron by
electron-builder
command , like this:electron-builder build --windows
, but it's not work. I need other parameter for the command? -
Sigma Octantis over 5 years@AlbertChen Try to put it in the root of source files, like in this repository that is used as example in the docs github.com/motdotla/dotenv-expand/tree/master/test (See
.env
) -
Finn over 5 yearsI was put
electron-builder.env
file in root of source files(same aspackage.json
path orelectron-builder.json
path). -
Sigma Octantis over 5 years@AlbertChen Wait, you want the variable to be defined at runtime in the built binaries? Because if it's that, the answer has nothing to do with electron-builder.
-
Finn over 5 yearsruntime?built binarie? I went the
env
variable is working in release Setup(.exe) file byelectron-builder
builder. -
Sigma Octantis over 5 yearsLet us continue this discussion in chat.
-
oyalhi over 4 yearsIt's stated in directly in the documentation. Create a
electron-builder.env
file and put the variables inside. Theelectron-builder.env
file should reside in the root of the folder, i.e. same folder as wherepackage.json
is, or whereelectron-builder.json
is (if you are using it). When you package the app using the command line interface, meaning runelectron-builder
to package the files rather than using it as a library. However, it does NOT work. If any one gets it to working, please do comment.