1

I am getting this error, I am using React/TypeScript

When I do this, In the conmponent.

<p>API_URL: {window._env_.API_URL}</p>

I get this error:

property '_env_' does not exist on type 'Window & typeof globalThis

Following this: https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-react-app-docker-and-nginx-7f9d42a91d70/

Sole
  • 3,100
  • 14
  • 58
  • 112
  • You need to declare it: [TypeScript error: Property 'X' does not exist on type 'Window'](https://stackoverflow.com/questions/56457935/typescript-error-property-x-does-not-exist-on-type-window) – CherryDT Nov 26 '21 at 15:01

2 Answers2

0

I know the question is old, posting answer for other. Very helpful.

declare const window: any;

export const MyComponent= () => {
  
  console.log("In Login");
  try {
    console.log("widow[API_URL]: " + window._env_.API_URL);
  } catch (error) {
    console.log(error);
  }

return (
    <div> Test DIV </div>
);
}
Abdul
  • 1,130
  • 4
  • 29
  • 65
-1

you have to use prefix (REACT_APP) and after for accessing the env file, you have to use proccess

<p>API_URL: {process.env.REACT_APP_API_URL}</p>
vahid ghadiri
  • 345
  • 1
  • 7
  • I have seen this before, although this is more to do with a Typescript error. Windows Type – Sole Nov 26 '21 at 14:58