---
title: "@std/dotenv"
description: "Parsing and loading environment variables from a `.env` file"
jsr: jsr:@std/dotenv
pkg: dotenv
version: 0.225.8
generated: true
stability: unstable
---
<!-- Autogenerated from JSR docs. Do not edit directly. -->

:::info Unstable

This @std package is experimental and its API may change without a major version bump.

:::
## Overview

<p>Parses and stringifies data in the <code>.env</code> file format.</p>
<p>Note: The key needs to match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/.</p>

```js
import { parse, stringify } from "@std/dotenv";
import { assertEquals } from "@std/assert";

assertEquals(parse("GREETING=hello world"), { GREETING: "hello world" });
assertEquals(stringify({ GREETING: "hello world" }), "GREETING='hello world'");
```

<h2 id="migrating-from-load()" class="anchorable"><a href="#migrating-from-load()" class="anchor" aria-label="Anchor" tabindex="-1" rel="nofollow">
</a>Migrating from <code>load()</code></h2>
<p><a href="https://github.com/denoland/std/blob/HEAD/@std/dotenv@0.225.8/doc/~/load" rel="nofollow"><code>load</code></a>, <a href="https://github.com/denoland/std/blob/HEAD/@std/dotenv@0.225.8/doc/~/loadSync" rel="nofollow"><code>loadSync</code></a> and the <code>@std/dotenv/load</code> side-effect
module are deprecated in favor of the runtime's
<a href="https://docs.deno.com/runtime/reference/env_variables/" rel="nofollow">--env-file</a>
flag, which Node.js and Bun also support:</p>

```js
deno run --env-file app.ts
deno run --env-file=.env --env-file=.env.local app.ts
```

<table>
<thead>
<tr>
<th>Deprecated API</th>
<th>Replacement</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>import "@std/dotenv/load"</code></td>
<td><code>deno run --env-file app.ts</code></td>
</tr>
<tr>
<td><code>load({ export: true })</code> / <code>loadSync({ export: true })</code></td>
<td><code>--env-file</code></td>
</tr>
<tr>
<td><code>load({ envPath: "./.env_prod" })</code></td>
<td><code>--env-file=.env_prod</code></td>
</tr>
<tr>
<td><code>load()</code> (read into an object, no export)</td>
<td><code>parse(await Deno.readTextFile(".env"))</code></td>
</tr>
</tbody>
</table>
<p>Differences to be aware of:</p>
<ul>
<li><code>load()</code> silently ignores a missing file. <code>--env-file</code> warns but continues,
while the <code>parse()</code> replacement above throws. To treat the file as
optional:</li>
</ul>

```js
import { parse } from "@std/dotenv";

let env: Record<string, string> = {};
try {
  env = parse(await Deno.readTextFile(".env"));
} catch (e) {
  if (!(e instanceof Deno.errors.NotFound)) throw e;
}
```

<ul>
<li><code>$VAR</code> inside a double-quoted value stays literal with <code>load()</code> but
expands with <code>--env-file</code>. Use single quotes for values containing a
literal <code>$</code>.</li>
<li><code>${KEY:-default}</code> and nested defaults are not supported by <code>--env-file</code>.
Use <a href="https://github.com/denoland/std/blob/HEAD/@std/dotenv@0.225.8/doc/~/parse" rel="nofollow"><code>parse</code></a>, which keeps the full expansion behavior.</li>
<li>Node's <code>--env-file</code> performs no variable expansion at all.</li>
<li>On platforms without a CLI, e.g. Deno Deploy, set environment variables
through the platform's configuration instead.</li>
</ul>

### Add to your project

```sh
deno add jsr:@std/dotenv
```

<a href="https://jsr.io/@std/dotenv/doc" class="docs-cta jsr-cta">See all symbols in @std/dotenv on
<svg class="inline ml-1" viewBox="0 0 13 7" aria-hidden="true" height="20"><path d="M0,2h2v-2h7v1h4v4h-2v2h-7v-1h-4" fill="#083344"></path><g fill="#f7df1e"><path d="M1,3h1v1h1v-3h1v4h-3"></path><path d="M5,1h3v1h-2v1h2v3h-3v-1h2v-1h-2"></path><path d="M9,2h3v2h-1v-1h-1v3h-1"></path></g></svg></a>

<!-- custom:start -->
<!-- Add persistent custom content below. This section is preserved across generations. -->

<!-- custom:end -->
