In the context of utilizing React, Typescript, Vite, and Yarn in my project, I am facing an issue with defining my backend URL as a constant in my .env file. Despite setting it up correctly, the frontend is still making requests to localhost:3036 instead of using the specified backend URL running on port 8080.
Initially, I defined the backend URL in my .env file like this:
NODE_ENV=development
VITE_BACKEND_URL=http://localhost:8080
Although I tried troubleshooting by prefixing it with VITE and following the instructions provided in the documentation, such as adding configuration in my vite.config.ts file, the problem persisted.
Upon further investigation, I attempted to access the backend URL using import.meta.env.VITE_BACKEND_URL
, but encountered errors indicating that import.meta.env
was undefined. Similarly, when checking process.env
, it displayed
BACKEND_URL from process.env: undefined
.
The method through which I make requests using the backend URL involves configuring Axios instances, which should ideally pick up the value set in the .env file. Running commands like yarn build
and yarn dev
while setting the NODE_ENV and BACKEND_URL also seem to function as expected, except for some CORS issues.
Despite verifying that the environment variables are accessible post-sourcing the .env file, the backend URL is not being recognized at the top-most level where vite loads the .env file. Any guidance or insights on resolving this issue would be greatly appreciated.