reactjs - Why is React production build code (npm run build) locking in environment variables and not updating on serve -s build

admin2025-04-17  3

I have a locally hosted project that I'm working on that utilizes a node server (accessing MongoDB), React JS frontend, and an Electron app (basically just a browser shell that connects to the React app) that connects from other local computers. I think this is a bit unorthodox but that's besides the point. On the server computer, the React app is hosted using the NPM serve package using "serve -s build" after executing npm run build on the React project. The React app, Electron app, and Node server reads an Windows environment variable which I have set to the server computer's IP address, which I access using: process.env.REACT_APP_SERVERHOST

The problem is with the React app, that environment variable seems to only be read on the initial "npm run build" command. If I change that environment variable in Windows, and change the IP address of the machine, then restart the React app "serve -s build", that value does not get updated in React. The node server and electron apps both read the new value.

Is there any way to get the process.env values to update with the new Windows environment variables without rebuilding the project?

转载请注明原文地址:http://www.anycun.com/QandA/1744893117a89115.html