You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using `import { Env } from '@env';` will import the env from the `src/core/env.js` file, which export client only env vars.
84
90
:::
85
91
86
-
5. Use `APP_ENV` to load the correct `.env` file :
92
+
6. Use `APP_ENV` to load the correct `.env` file :
87
93
88
94
```bash
89
95
APP_ENV=production pnpm start -cc
@@ -98,28 +104,28 @@ As mentioned earlier, `zod` is used to validate environment variables at runtime
98
104
```
99
105
100
106
:::note[Important]
101
-
Updating `env` files will require running `pnpm prebuild` to load the new values
107
+
As `dotenv` handles variables as strings, you need to convert them to the correct typewhile reading them from the `process.env` object. check the example below for more details on how to use numbers and boolean values.
102
108
:::
103
109
104
110
## How it works
105
111
106
112
#### ✅ Validate and parse environment variables
107
113
108
-
If you take a look at the `env.js` file, you will notice that the file is split into tree main parts as shown below:
114
+
If you take a look at the `env.js` file, you will notice that the file is split into three main parts as shown below:
109
115
110
116
<Code file="env.js" />
111
117
112
118
**In the first part** We load the correct `.env` file based on the `APP_ENV` variable using `dotenv` package. If the `APP_ENV` variable is not defined, we default to `development`.
113
119
114
-
we define some static variables forthe app such as the app name, bundle id and package. While these variables can be added to the `.env` files, we recommend keeping themin the `env.js` file as they are not meant to change. To handle different app variants, you can add suffixes to these variables using the `withEnvSuffix` function.
120
+
we define some static variables forthe app such as the app name, bundle Id and package. While these variables can be added to the `.env` files, we recommend keeping themin the `env.js` file as they are not meant to change. To handle different app variants, you can add suffixes to these variables using the `withEnvSuffix` function.
115
121
116
122
**In the second part**, we define the `zod` schema for the environment variables.
117
123
118
124
We split the environment variables into two parts:
119
125
120
126
- **Client Variables**: Variables that are safe to be exposed to the client and used in the `src` folder.
121
127
122
-
- **Build Time Variables**: Variables that we don't need on the client-side and are only used in the `app.config.ts`, for example `SENTRY_AUTH` to upload the source maps to Sentry.
128
+
- **Build Time Variables**: Variables that we don't need on the client-side and are only used in the `app.config.ts`, for example, `SENTRY_AUTH` to upload the source maps to Sentry.
123
129
124
130
These schemas are used to validate the environment variables. All the environment variables should be added to the correct schema.
125
131
@@ -129,7 +135,7 @@ We use the `z.infer` utility to infer the environment variables' types from the
129
135
130
136
#### ✅ Use and send environment variables to the client
131
137
132
-
Now its easy as importing `Env` , `ClientEnv` and `withEnvSuffix` from the `./env.js` file and use inside our `app.config.ts`, and finally send client env vars to the client side using `extra` property.
138
+
Now it's as easy as importing `Env` , `ClientEnv` and `withEnvSuffix` from the `./env.js` file and use inside our `app.config.ts`, and finally sending client env vars to the client side using `extra` property.
0 commit comments