The .env
file which contains the environmental variables and is located in the root directory of our project. We want to get the link to our API from our env file. Here is what our env file would look like:
VITE_API_URL=https://api.someapp.somecompany.com
Now, in our React app, we import the URL like so:
const API_URL: string = import.meta.env.VITE_API_URL;
NOTE: Variables in the env file must start with VITE
in order for Vite to find them. Otherwise, Vite will return undefined
.