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

environment variables in node.js-apps #77

Merged
merged 5 commits into from
Feb 12, 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
36 changes: 35 additions & 1 deletion docs/technologies/languages/nodejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,38 @@ If your project uses the [Yarn PnP](https://yarnpkg.com/features/pnp) installati

```yml title=".yarnrc"
enableGlobalCache: false
```
```

## Environment Variables

Environment variables are generally used to configure Node.js applications. Depending on the Node.js version, we recommend different variants to define these.

<Tabs groupId="environment-variables">
<TabItem value="below-20-6" label="< Node.js 20.6">
You can define environment variables in the file `~/.config/node/.env`. Variables defined there are then automatically available in your application.

</TabItem>
<TabItem value="from-20-6" label=">= Node.js 20.6">
As of Node.js 20.6, an (experimental) flag `--env-file` is available (see [Node.js documentation](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig)). This can be specified as often as required to load .env files whose environment variables are then available in the app.

To use the flag, configure the start command of your Node.js app as follows, for example:

```bash
node --env-file=.env server.js
```

The path to the `.env` file is specified relative to the working directory.

If you use an npm or yarn script to start your application, the following example applies:

```json title="package.json"
{
[..]
"scripts": {
"start": "node --env-file=.env server.js"
}
[..]
}
```
</TabItem>
</Tabs>
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,38 @@ Wenn dein Projekt [Yarn PnP](https://yarnpkg.com/features/pnp) nutzt, ist es emp

```yml title=".yarnrc"
enableGlobalCache: false
```
```

## Umgebungsvariablen

In der Regel werden Umgebungsvariablen genutzt, um Node.js-Apps zu konfigurieren. Je nach Node.js-Version empfehlen wir unterschiedliche Varianten, um diese zu definieren.

<Tabs groupId="environment-variables">
<TabItem value="below-20-6" label="< Node.js 20.6">
Du kannst Umgebungsvariablen in der Datei `~/.config/node/.env` definieren. Dort definierte Variablen sind dann automatisch in deiner App verfügbar.

</TabItem>
<TabItem value="from-20-6" label=">= Node.js 20.6">
Ab Node.js 20.6 steht ein (experimentelles) Flag `--env-file` zur Verfügung (vgl. [Node.js-Dokumentation](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig)). Dieses kann beliebig oft angegeben werden, um .env-Dateien zu laden, deren Umgebungsvariablen dann in der App verfügbar sind.

Um das Flag zu nutzen, konfiguriere den Startbefehl deiner Node.js-App beispielsweise folgendermaßen:

```bash
node --env-file=.env server.js
```

Der Pfad zur `.env`-Datei wird dabei relativ zum Working Directory angegeben.

Nutzt du ein npm- oder yarn-Script zum Start deiner App, gilt folgendes Beispiel:

```json title="package.json"
{
[..]
"scripts": {
"start": "node --env-file=.env server.js"
}
[..]
}
```
</TabItem>
</Tabs>