Variable in ENV file undefined

Viewed 30

I am trying to access a variable in my .env file. Unfortunately, I am getting an undefined back.

.env file

REACT_APP_SOCKET_URL = ws://localhost:2900

attempt to access it

useEffect(() => {
 console.log(process.env.REACT_APP_SOCKET_URL); //undefined
},[]);
1 Answers
  1. Make sure you're using an up-to-date version of create-react-app, using .env hasn't always been supported https://create-react-app.dev/docs/adding-custom-environment-variables
  2. Make sure .env is located in the project root where package.json is.
  3. Try not having spaces in .env, try putting the value in double quotes ("ws://localhost:2900")
  4. Restart the dev server, changes to .env aren't picked up automatically as it works for hot module reloading
Related