Skip to main content

Environment Variables

Environment variables can be accessed within a React app from the process.env object (e.g. process.env.NODE_ENV to determine whether the app is running in a ‘development’ environment or a ‘production’ environment).

Set Environment Variables

.env Files

https://create-react-app.dev/docs/adding-custom-environment-variables/

Create .env (or .env.development / .env.production / .env.development.local, .env.test, etc.) at the root of the React project and the variables will be available within the JS code using process.env.[varName].

.env

# Application's version from package.json
REACT_APP_VERSION=$npm_package_version

You can get the environment variable’s value in the JavaScript code using process.env.

process.env.NODE_ENV === 'development'

.env.development

# ServiceNow user account authentication intormation. 
# Done update the values for REACT_APP_USER and REACT_APP_PASSWORD in this file. Create env.development.local and set your specific values there.
# NOTE: If a variable value has the '$' character, be sure to escape it as '\\$' in .env.development.local
REACT_APP_USER='username'
REACT_APP_PASSWORD='username-password'

Command Line (within package.json)

Setting an environment variable in a package.json script tag…

"scripts": {
"start-set-port": "set PORT=3002 && react-scripts-start"
}