Home > other >  process.env environment variable undefined
process.env environment variable undefined

Time:02-17

I am trying to access an environment variable through process.env in my React/Typescript code, but it's returning undefined. I am able to access NODE_ENV though.

console.log(process.env.CURRENT_URL) // prints "undefined" on browser
console.log(process.env.NODE_ENV) // prints "development" on browser

CURRENT_URL is present in windows environment variable.

Why is process.env.NODE_ENV is accessible but not the other variable?

CodePudding user response:

This is your answer, from the official create-react-app docs.

Summary:
By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_.

So append REACT_APP_ to all your env variables e.g. REACT_APP_CURRENT_URL.
That should work!

CodePudding user response:

You can use this package to defined the process env

cross-env

  • Related