Env setup and usage
Setup:
env.jsfile at the root of your project.- This file is responsible for:
- Loading environment variables from the
.envfile. - Defining a schema using Zod to ensure the validity and type safety of the expected environment variables.
- Validating the loaded variables against the schema.
- Exposing the validated and parsed environment variables as
Envfor use in your application.
- Loading environment variables from the
- This file is responsible for:
app.config.tsfile at the root of your project.- Purpose: This file extends the Expo configuration (
ExpoConfig) by incorporating the validated environment variables fromenv.js. - Functionality:
- Imports the
Envobject fromenv.js, which contains the validated environment variables. - This effectively injects the environment variables (
Env) into theextraproperty of the Expo configuration. - Exports the modified Expo configuration for use during the app build process.
- Imports the
- Purpose: This file extends the Expo configuration (
- folder
configwith a file namedenv.jsinside it.config/env.jsleveragesexpo-constantsto fetch the config fromapp.config.ts.
Using Env
Now, instead of using process.env in your files, you can directly use the exported Env from config/env.js.
Example:
- Instead of using
process.env.EXPO_PUBLIC_GRAPHQL_URL, you can useEnv.EXPO_PUBLIC_GRAPHQL_URL.