On this page
@std/dotenv
Unstable
This @std package is experimental and its API may change without a major version bump.
Overview Jump to heading
Parses and stringifies data in the .env file format.
Note: The key needs to match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/.
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'");
Migrating from load()
load, loadSync and the @std/dotenv/load side-effect
module are deprecated in favor of the runtime's
--env-file
flag, which Node.js and Bun also support:
deno run --env-file app.ts
deno run --env-file=.env --env-file=.env.local app.ts
| Deprecated API | Replacement |
|---|---|
import "@std/dotenv/load" |
deno run --env-file app.ts |
load({ export: true }) / loadSync({ export: true }) |
--env-file |
load({ envPath: "./.env_prod" }) |
--env-file=.env_prod |
load() (read into an object, no export) |
parse(await Deno.readTextFile(".env")) |
Differences to be aware of:
load()silently ignores a missing file.--env-filewarns but continues, while theparse()replacement above throws. To treat the file as optional:
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;
}
$VARinside a double-quoted value stays literal withload()but expands with--env-file. Use single quotes for values containing a literal$.${KEY:-default}and nested defaults are not supported by--env-file. Useparse, which keeps the full expansion behavior.- Node's
--env-fileperforms no variable expansion at all. - On platforms without a CLI, e.g. Deno Deploy, set environment variables through the platform's configuration instead.
Add to your project Jump to heading
›_
deno add jsr:@std/dotenv