electron-builder, how to set node environmental variables

12,812

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.

Share:
12,812
Finn
Author by

Finn

Updated on June 07, 2022

Comments

  • Finn
    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 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.

  • Finn
    Finn over 5 years
    What does “CLI usage” mean? I created a electron-builder.env file in the current dir and input NODE_ENV=production, and builder electron, It's not work. I try to working in electron . to start and test show console.log(process.env.NODE_ENV), It's undefined.
  • Sigma Octantis
    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
    Finn over 5 years
    I 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
    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
    Finn over 5 years
    I was put electron-builder.env file in root of source files(same as package.json path or electron-builder.json path).
  • Sigma Octantis
    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
    Finn over 5 years
    runtime?built binarie? I went the env variable is working in release Setup(.exe) file by electron-builder builder.
  • Sigma Octantis
    Sigma Octantis over 5 years
  • oyalhi
    oyalhi over 4 years
    It's stated in directly in the documentation. Create a electron-builder.env file and put the variables inside. The electron-builder.env file should reside in the root of the folder, i.e. same folder as where package.json is, or where electron-builder.json is (if you are using it). When you package the app using the command line interface, meaning run electron-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.