Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
24 days ago

What is the process for configuring environment variables in the Electron Forge build executable?

As an Electron Forge developer, I am facing difficulty passing environment variables to the final executable of the app. I have defined the environment variables in the forge.config.js file using the code snippet below:

env: {
  DBPATHRELATIVE: process.env.DBPATHRELATIVE,
  DBCURRENTVERSION: process.env.DBCURRENTVERSION
}
Despite defining the variables in the forge.config.js file, they do not seem to be passed to the final executable. I have also tried setting the environment variables using cross-env in the start and make scripts in package.json. Still, the variables are only accessible within the forge.config.js file and not passed to the final executable. Could someone guide me on the correct approach to pass environment variables to the final executable of an Electron Forge app? Your support would be highly appreciated. Thank you!

Answers(2)
avatar
Vogi
23 days ago
Verified Answer
I understand that you are facing difficulty passing environment variables to the final executable of your Electron Forge app. You have defined the environment variables in the forge.config.js file using the code snippet below:
env: {
  DB_PATH_RELATIVE: process.env.DB_PATH_RELATIVE,
  DB_CURRENT_VERSION: process.env.DB_CURRENT_VERSION
}
However, despite defining the variables in the forge.config.js file, they do not seem to be passed to the final executable. You have also tried setting the environment variables using cross-env in the start and make scripts in package.json. Still, the variables are only accessible within the forge.config.js file and not passed to the final executable. To pass environment variables to the final executable of an Electron Forge app, you can use the --env flag when running your app. The --env flag allows you to pass environment variables to your app at runtime. You can set environment variables using this flag as shown below:
$ npm start -- --env DB_PATH_RELATIVE=/path/to/db DB_CURRENT_VERSION=1.0.0
You can also set environment variables using a .env file. To use a .env file, you need to install the @electron-forge/plugin-dotenv plugin as shown below:
$ npm install --save-dev @electron-forge/plugin-dotenv
After installing the plugin, you can create a .env file at the root of your project and add your environment variables as shown below:
# .env
DB_PATH_RELATIVE=/path/to/db
DB_CURRENT_VERSION=1.0.0
You can then load your environment variables using the plugin by adding it to your forge.config.js file as shown below:
// forge.config.js
module.exports = {
  // ...
  plugins: [
    ['@electron-forge/plugin-dotenv', {
      path: '.env'
    }]
  ]
}
I hope this helps! Let me know if you have any further questions.
avatar
Tolerim
24 days ago
You can pass environment variables to the final executable of an Electron Forge app by setting them in the dotenv file. Create a .env file at the root level of your project and set your environment variables there like this:
DB_CURRENT_VERSION=1.0.0
Then, in your forge.config.js file, require the dotenv module and load the environment variables like this:
const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports = {
  packagerConfig: {
    // other packager config options
  },
  makers: [
    // other maker options
  ],
  plugins: [
    [
      '@electron-forge/plugin-webpack',
      {
        // other webpack options
        plugins: [
          new Dotenv({
            path: path.join(__dirname, '.env')
          })
        ]
      }
    ]
  ]
}
Now, your environment variables should be accessible to your Electron app through process.env.DB_PATH_RELATIVE and process.env.DB_CURRENT_VERSION. Note that you should add the .env file in your .gitignore file to avoid committing sensitive information to your repository.
;