1 import { Table } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Environment Variables`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how environment variables are loaded in Medusa.
10
11 ## System Environment Variables
12
13 The Medusa application loads and uses system environment variables.
14
15 For example, if you set the `PORT` environment variable to `8000`, the Medusa application runs on that port instead of `9000`.
16
17 In production, you should always use system environment variables that you set through your hosting provider.
18
19 ---
20
21 ## Environment Variables in .env Files
22
23 During development, it's easier to set environment variables in a `.env` file in your repository.
24
25 Based on your `NODE_ENV` system environment variable, Medusa will try to load environment variables from the following `.env` files:
26
27 <Note title="Tip">
28
29 As of [Medusa v2.5.0](https://github.com/medusajs/medusa/releases/tag/v2.5.0), `NODE_ENV` defaults to `production` when using `medusa start`. Otherwise, it defaults to `development`.
30
31 </Note>
32
33 <Table>
34 <Table.Header>
35 <Table.Row>
36 <Table.HeaderCell>
37 Environment
38 </Table.HeaderCell>
39 <Table.HeaderCell>
40
41 `.env` File
42
43 </Table.HeaderCell>
44 </Table.Row>
45 </Table.Header>
46 <Table.Body>
47 <Table.Row>
48 <Table.Cell>
49
50 `NODE_ENV` = `development` or not set
51
52 </Table.Cell>
53 <Table.Cell>
54
55 `.env`
56
57 </Table.Cell>
58 </Table.Row>
59 <Table.Row>
60 <Table.Cell>
61
62 `NODE_ENV` = `production`
63
64 </Table.Cell>
65 <Table.Cell>
66
67 `.env.production`
68
69 </Table.Cell>
70 </Table.Row>
71 <Table.Row>
72 <Table.Cell>
73
74 `NODE_ENV` = `staging`
75
76 </Table.Cell>
77 <Table.Cell>
78
79 `.env.staging`
80
81 </Table.Cell>
82 </Table.Row>
83 <Table.Row>
84 <Table.Cell>
85
86 `NODE_ENV` = `test`
87
88 </Table.Cell>
89 <Table.Cell>
90
91 `.env.test`
92
93 </Table.Cell>
94 </Table.Row>
95 </Table.Body>
96 </Table>
97
98 ### Set Environment in `loadEnv`
99
100 In the `medusa-config.ts` file of your Medusa application, you'll find a `loadEnv` function used that accepts `process.env.NODE_ENV` as a first parameter.
101
102 This function is responsible for loading the correct `.env` file based on the value of `process.env.NODE_ENV`.
103
104 To ensure that the correct `.env` file is loaded as shown in the table above, only specify `development`, `production`, `staging` or `test` as the value of `process.env.NODE_ENV` or as the parameter of `loadEnv`.
105
106 ---
107
108 ## Environment Variables for Admin Customizations
109
110 Since the Medusa Admin is built on top of [Vite](https://vite.dev/), you prefix the environment variables you want to use in a widget or UI route with `VITE_`. Then, you can access or use them with the `import.meta.env` object.
111
112 Learn more in the [Admin Environment Variables](../admin/environment-variables/page.mdx) chapter.
113
114 ---
115
116 ## Predefined Medusa Environment Variables
117
118 You can further customize the Medusa application behavior using the following predefined environment variables. You can set the environment variables in your `.env` file or in your deployment platform.
119
120 <Note title="Tip">
121
122 You should opt for setting configurations in `medusa-config.ts` where possible. For a full list of Medusa configurations, refer to the [Medusa Configurations chapter](../../configurations/medusa-config/page.mdx).
123
124 </Note>
125
126 <Table>
127 <Table.Header>
128 <Table.Row>
129 <Table.HeaderCell>
130 Environment Variable
131 </Table.HeaderCell>
132 <Table.HeaderCell>
133 Description
134 </Table.HeaderCell>
135 <Table.HeaderCell>
136 Default
137 </Table.HeaderCell>
138 </Table.Row>
139 </Table.Header>
140 <Table.Body>
141 <Table.Row>
142 <Table.Cell>
143 `HOST`
144 </Table.Cell>
145 <Table.Cell>
146 The host to run the Medusa application on.
147 </Table.Cell>
148 <Table.Cell>
149 `localhost`
150 </Table.Cell>
151 </Table.Row>
152 <Table.Row>
153 <Table.Cell>
154 `PORT`
155 </Table.Cell>
156 <Table.Cell>
157 The port to run the Medusa application on.
158 </Table.Cell>
159 <Table.Cell>
160 `9000`
161 </Table.Cell>
162 </Table.Row>
163 <Table.Row>
164 <Table.Cell>
165 `DATABASE_URL`
166 </Table.Cell>
167 <Table.Cell>
168 The URL to connect to the PostgreSQL database. Only used if [projectConfig.databaseUrl](../../configurations/medusa-config/page.mdx#databaseurl) isn't set in `medusa-config.ts`.
169 </Table.Cell>
170 <Table.Cell>
171 `postgres://localhost/medusa-starter-default`
172 </Table.Cell>
173 </Table.Row>
174 <Table.Row>
175 <Table.Cell>
176 `STORE_CORS`
177 </Table.Cell>
178 <Table.Cell>
179 URLs of storefronts that can access the Medusa backend's Store APIs. Only used if [projectConfig.http.storeCors](../../configurations/medusa-config/page.mdx#http-storeCors-1-1) isn't set in `medusa-config.ts`.
180 </Table.Cell>
181 <Table.Cell>
182 `http://localhost:8000`
183 </Table.Cell>
184 </Table.Row>
185 <Table.Row>
186 <Table.Cell>`ADMIN_CORS`</Table.Cell>
187 <Table.Cell>
188 URLs of admin dashboards that can access the Medusa backend's Admin APIs. Only used if [projectConfig.http.adminCors](../../configurations/medusa-config/page.mdx#http-adminCors-1-2) isn't set in `medusa-config.ts`.
189 </Table.Cell>
190 <Table.Cell>
191 `http://localhost:7000,http://localhost:7001,http://localhost:5173`
192 </Table.Cell>
193 </Table.Row>
194 <Table.Row>
195 <Table.Cell>`AUTH_CORS`</Table.Cell>
196 <Table.Cell>
197 URLs of clients that can access the Medusa backend's authentication routes. Only used if [projectConfig.http.authCors](../../configurations/medusa-config/page.mdx#http-authCors-1-0) isn't set in `medusa-config.ts`.
198 </Table.Cell>
199 <Table.Cell>
200 `http://localhost:7000,http://localhost:7001,http://localhost:5173`
201 </Table.Cell>
202 </Table.Row>
203 <Table.Row>
204 <Table.Cell>`JWT_SECRET`</Table.Cell>
205 <Table.Cell>
206 A random string used to create authentication tokens in the http layer. Only used if [projectConfig.http.jwtSecret](../../configurations/medusa-config/page.mdx#http-jwtSecret-1-3) isn't set in `medusa-config.ts`.
207 </Table.Cell>
208 <Table.Cell>
209 \-
210 </Table.Cell>
211 </Table.Row>
212 <Table.Row>
213 <Table.Cell>
214 `COOKIE_SECRET`
215 </Table.Cell>
216 <Table.Cell>
217 A random string used to create cookie tokens in the http layer. Only used if [projectConfig.http.cookieSecret](../../configurations/medusa-config/page.mdx#http-cookieSecret-1-5) isn't set in `medusa-config.ts`.
218 </Table.Cell>
219 <Table.Cell>
220 \-
221 </Table.Cell>
222 </Table.Row>
223 <Table.Row>
224 <Table.Cell>
225 `MEDUSA_BACKEND_URL`
226 </Table.Cell>
227 <Table.Cell>
228 The URL to the Medusa backend. Only used if [admin.backendUrl](../../configurations/medusa-config/page.mdx#backendurl) isn't set in `medusa-config.ts`.
229 </Table.Cell>
230 <Table.Cell>
231 \-
232 </Table.Cell>
233 </Table.Row>
234 <Table.Row>
235 <Table.Cell>
236 `DB_HOST`
237 </Table.Cell>
238 <Table.Cell>
239 The host for the database. It's used when generating migrations for a plugin, and when running integration tests.
240 </Table.Cell>
241 <Table.Cell>
242 `localhost`
243 </Table.Cell>
244 </Table.Row>
245 <Table.Row>
246 <Table.Cell>
247 `DB_USERNAME`
248 </Table.Cell>
249 <Table.Cell>
250 The username for the database. It's used when generating migrations for a plugin, and when running integration tests.
251 </Table.Cell>
252 <Table.Cell>
253 \-
254 </Table.Cell>
255 </Table.Row>
256 <Table.Row>
257 <Table.Cell>
258 `DB_PASSWORD`
259 </Table.Cell>
260 <Table.Cell>
261 The password for the database user. It's used when generating migrations for a plugin, and when running integration tests.
262 </Table.Cell>
263 <Table.Cell>
264 \-
265 </Table.Cell>
266 </Table.Row>
267 <Table.Row>
268 <Table.Cell>
269 `DB_TEMP_NAME`
270 </Table.Cell>
271 <Table.Cell>
272 The database name to create for integration tests.
273 </Table.Cell>
274 <Table.Cell>
275 \-
276 </Table.Cell>
277 </Table.Row>
278 <Table.Row>
279 <Table.Cell>
280 `LOG_LEVEL`
281 </Table.Cell>
282 <Table.Cell>
283 The allowed levels to log. Learn more in the [Logging](../../debugging-and-testing/logging/page.mdx#log-levels) chapter.
284 </Table.Cell>
285 <Table.Cell>
286 `silly`
287 </Table.Cell>
288 </Table.Row>
289 <Table.Row>
290 <Table.Cell>
291 `LOG_FILE`
292 </Table.Cell>
293 <Table.Cell>
294 The file to save logs in. By default, logs aren't saved in any file. Learn more in the [Logging](../../debugging-and-testing/logging/page.mdx) chapter.
295 </Table.Cell>
296 <Table.Cell>
297 \-
298 </Table.Cell>
299 </Table.Row>
300 <Table.Row>
301 <Table.Cell>
302 `MEDUSA_DISABLE_TELEMETRY`
303 </Table.Cell>
304 <Table.Cell>
305 Whether to disable analytics data collection. Learn more in the [Usage](../../resources/usage/page.mdx) chapter.
306 </Table.Cell>
307 <Table.Cell>
308 \-
309 </Table.Cell>
310 </Table.Row>
311 <Table.Row>
312 <Table.Cell>
313
314 `ADMIN_AUTH_TYPE` ([v2.12.0+](https://github.com/medusajs/medusa/releases/tag/v2.12.0))
315
316 </Table.Cell>
317 <Table.Cell>
318
319 A string indicating the authentication method that the JS SDK instance uses in the Medusa Admin. Possible values are `session` and `jwt`. Learn more in the [JS SDK Authentication guide](!resources!/js-sdk/auth/overview).
320
321 </Table.Cell>
322 <Table.Cell>
323
324 `session`
325
326 </Table.Cell>
327 </Table.Row>
328 <Table.Row>
329 <Table.Cell>
330
331 `ADMIN_JWT_TOKEN_STORAGE_KEY` ([v2.12.0+](https://github.com/medusajs/medusa/releases/tag/v2.12.0))
332
333 </Table.Cell>
334 <Table.Cell>
335
336 A string indicating the key used to store the authentication JWT token in the browser's local storage. Only applicable if `ADMIN_AUTH_TYPE` is set to `jwt`.
337
338 </Table.Cell>
339 <Table.Cell>
340
341 `medusa_auth_token`
342
343 </Table.Cell>
344 </Table.Row>
345 </Table.Body>
346 </Table>