乐闻世界logo
搜索文章和话题

How to set node environmental variables in Electron builder?

1个答案

1

There are several ways to set Node environment variables in Electron, and the choice depends on the specific application requirements and development environment. Here are several common methods for setting and using Node environment variables:

1. Setting Environment Variables When Launching Electron

When launching the Electron application from the command line, you can directly set environment variables. For example, on Windows systems, you can use the following command:

bash
set NODE_ENV=production && electron .

On macOS or Linux systems, the command is:

bash
NODE_ENV=production electron .

This method is suitable for temporary modifications or quickly testing different environment configurations during development.

2. Dynamically Setting Environment Variables in the Main Process

In the Electron main process main.js file, you can use the Node.js process.env object to set environment variables. For example:

javascript
process.env.NODE_ENV = 'production'; const { app, BrowserWindow } = require('electron'); app.on('ready', () => { let mainWindow = new BrowserWindow({ // Window configuration }); mainWindow.loadURL('http://example.com'); // Other code });

This method allows dynamically setting environment variables based on different conditions when the application starts.

3. Using .env Files

For complex applications requiring multiple environment variables, using .env files provides centralized configuration management. This requires leveraging a library like dotenv to load settings from .env files.

First, install dotenv:

bash
npm install dotenv

Then, create a .env file in the project root directory, for example:

shell
NODE_ENV=production API_URL=http://example.com/api

Load this configuration in the main process:

javascript
require('dotenv').config(); const { app, BrowserWindow } = require('electron'); console.log(process.env.NODE_ENV); // Outputs 'production' console.log(process.env.API_URL); // Outputs 'http://example.com/api' app.on('ready', () => { let mainWindow = new BrowserWindow({ // Window configuration }); mainWindow.loadURL(process.env.API_URL); // Other code });

Using .env files simplifies managing and switching between different environment configurations, while also enhancing code clarity and maintainability.

Example Application Scenario

Suppose you are developing a desktop application for an e-commerce platform that connects to different API servers based on the environment (development, testing, production). You can control the server address by setting the API_URL environment variable and use .env files to manage these variables, enabling quick configuration switching across development stages to improve efficiency and stability.

These are several methods for setting Node environment variables in Electron. Choose the appropriate method based on your specific requirements to effectively manage environment variables.

2024年6月29日 12:07 回复

你的答案