'Replace `process.env.XXX` with environment variables
I am building a React app where I need to use environment variables set in .env within javascript files hosted under the public folder (e.g. /public/sw.js).
Ideally, within .env I could have:
MY_URL="Hello world"
Then within /public/sw.js I could use:
console.log(process.env.MY_URL);
When I run yarn start I would want console.log(process.env.MY_URL) to be replaced with console.log("MY_URL") before SPA is served.
My needs are really that straightforward.
The server this app will eventually be hosted on will not have a .env file which is why my preference would be the simple swap and replace value shown above. My expectation is that this is a one-time action each time the application is run or built; meaning yarn start or yarn build can replace /public/sw.js (or any other .js file containing a process.env reference.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
