Skip to content

Commit df5dc65

Browse files
authored
docs: added documentation on environment variables (medusajs#9798)
1 parent c4cd2c2 commit df5dc65

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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, which is considered as `development` if not set, Medusa will try to load environment variables from the following `.env` files:
26+
27+
<Table>
28+
<Table.Header>
29+
<Table.Row>
30+
<Table.HeaderCell>Environment</Table.HeaderCell>
31+
<Table.HeaderCell>
32+
33+
`.env` File
34+
35+
</Table.HeaderCell>
36+
</Table.Row>
37+
</Table.Header>
38+
<Table.Body>
39+
<Table.Row>
40+
<Table.Cell>
41+
42+
`NODE_ENV` = `development` or not set
43+
44+
</Table.Cell>
45+
<Table.Cell>
46+
47+
`.env`
48+
49+
</Table.Cell>
50+
</Table.Row>
51+
<Table.Row>
52+
<Table.Cell>
53+
54+
`NODE_ENV` = `production`
55+
56+
</Table.Cell>
57+
<Table.Cell>
58+
59+
`.env.production`
60+
61+
</Table.Cell>
62+
</Table.Row>
63+
<Table.Row>
64+
<Table.Cell>
65+
66+
`NODE_ENV` = `staging`
67+
68+
</Table.Cell>
69+
<Table.Cell>
70+
71+
`.env.staging`
72+
73+
</Table.Cell>
74+
</Table.Row>
75+
<Table.Row>
76+
<Table.Cell>
77+
78+
`NODE_ENV` = `test`
79+
80+
</Table.Cell>
81+
<Table.Cell>
82+
83+
`.env.test`
84+
85+
</Table.Cell>
86+
</Table.Row>
87+
</Table.Body>
88+
</Table>
89+
90+
### Set Environment in `loadEnv`
91+
92+
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.
93+
94+
This function is responsible for loading the correct `.env` file based on the value of `process.env.NODE_ENV`.
95+
96+
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`.

www/apps/book/generated/edit-dates.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@ export const generatedEditDates = {
112112
"app/learn/architecture/overview/page.mdx": "2024-09-23T12:55:01.339Z",
113113
"app/learn/advanced-development/data-models/infer-type/page.mdx": "2024-09-30T08:43:53.123Z",
114114
"app/learn/advanced-development/custom-cli-scripts/seed-data/page.mdx": "2024-10-03T11:11:07.181Z",
115-
"app/learn/basics/modules/page.mdx": "2024-10-16T08:49:39.997Z"
115+
"app/learn/basics/modules/page.mdx": "2024-10-16T08:49:39.997Z",
116+
"app/learn/advanced-development/environment-variables/page.mdx": "2024-10-25T14:59:07.831Z"
116117
}

www/apps/book/sidebar.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ export const sidebar = numberSidebarItems(
507507
},
508508
],
509509
},
510+
{
511+
type: "link",
512+
title: "Environment Variables",
513+
path: "/learn/advanced-development/environment-variables"
514+
}
510515
],
511516
},
512517
{

0 commit comments

Comments
 (0)