Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: added documentation on environment variables #9798

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Table } from "docs-ui"

export const metadata = {
title: `${pageNumber} Environment Variables`,
}

# {metadata.title}

In this chapter, you'll learn how environment variables are loaded in Medusa.

## System Environment Variables

The Medusa application loads and uses system environment variables.

For example, if you set the `PORT` environment variable to `8000`, the Medusa application runs on that port instead of `9000`.

In production, you should always use system environment variables that you set through your hosting provider.

---

## Environment Variables in .env Files

During development, it's easier to set environment variables in a `.env` file in your repository.

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:

<Table>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Environment</Table.HeaderCell>
<Table.HeaderCell>

`.env` File

</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>

`NODE_ENV` = `development` or not set

</Table.Cell>
<Table.Cell>

`.env`

</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>

`NODE_ENV` = `production`

</Table.Cell>
<Table.Cell>

`.env.production`

</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>

`NODE_ENV` = `staging`

</Table.Cell>
<Table.Cell>

`.env.staging`

</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>

`NODE_ENV` = `test`

</Table.Cell>
<Table.Cell>

`.env.test`

</Table.Cell>
</Table.Row>
</Table.Body>
</Table>

### Set Environment in `loadEnv`

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.

This function is responsible for loading the correct `.env` file based on the value of `process.env.NODE_ENV`.

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`.
3 changes: 2 additions & 1 deletion www/apps/book/generated/edit-dates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ export const generatedEditDates = {
"app/learn/architecture/overview/page.mdx": "2024-09-23T12:55:01.339Z",
"app/learn/advanced-development/data-models/infer-type/page.mdx": "2024-09-30T08:43:53.123Z",
"app/learn/advanced-development/custom-cli-scripts/seed-data/page.mdx": "2024-10-03T11:11:07.181Z",
"app/learn/basics/modules/page.mdx": "2024-10-16T08:49:39.997Z"
"app/learn/basics/modules/page.mdx": "2024-10-16T08:49:39.997Z",
"app/learn/advanced-development/environment-variables/page.mdx": "2024-10-25T14:59:07.831Z"
}
5 changes: 5 additions & 0 deletions www/apps/book/sidebar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ export const sidebar = numberSidebarItems(
},
],
},
{
type: "link",
title: "Environment Variables",
path: "/learn/advanced-development/environment-variables"
}
],
},
{
Expand Down
Loading