Skip to content

Commit

Permalink
Merge pull request #77 from mittwald/nodejs-environment-variables
Browse files Browse the repository at this point in the history
environment variables in node.js-apps
  • Loading branch information
patrickhilker authored Feb 12, 2024
2 parents 80af2bb + 6731dc9 commit 7f863b8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
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>

0 comments on commit 7f863b8

Please sign in to comment.