This release is versions behind 0.225.7 — the latest version of @std/dotenv.
Works with
•JSR Score94%•This package works with DenoIt is unknown whether this package works with Bun

Downloads49,457/wk
•Published2 years ago (0.196.0)
UNSTABLE: Parsing and loading environment variables from a `.env` file
Load environment variables from .env files.
Inspired by the node module dotenv and
dotenv-expand.
# .env GREETING=hello world
Then import the configuration using the load function.
// app.ts import { load } from "@std/dotenv"; console.log(await load());
Then run your app.
> deno run --allow-env --allow-read app.ts { GREETING: "hello world" }
Auto loading
load.ts automatically loads the local .env file on import and exports it to
the process environment:
# .env GREETING=hello world
// app.ts import "@std/dotenv/load"; console.log(Deno.env.get("GREETING"));
> deno run --allow-env --allow-read app.ts hello world
Parsing Rules
The parsing engine currently supports the following rules:
- Variables that already exist in the environment are not overridden with
export: true BASIC=basicbecomes{ BASIC: "basic" }- empty lines are skipped
- lines beginning with
#are treated as comments - empty values become empty strings (
EMPTY=becomes{ EMPTY: "" }) - single and double quoted values are escaped (
SINGLE_QUOTE='quoted'becomes{ SINGLE_QUOTE: "quoted" }) - new lines are expanded in double quoted values (
MULTILINE="new\nline"becomes
{ MULTILINE: "new\nline" }
- inner quotes are maintained (think JSON) (
JSON={"foo": "bar"}becomes{ JSON: "{\"foo\": \"bar\"}" }) - whitespace is removed from both ends of unquoted values (see more on
trim) (FOO= some valuebecomes{ FOO: "some value" }) - whitespace is preserved on both ends of quoted values (
FOO=" some value "becomes{ FOO: " some value " }) - dollar sign with an environment key in or without curly braces in unquoted
values will expand the environment key (
KEY=$KEYorKEY=${KEY}becomes{ KEY: "<KEY_VALUE_FROM_ENV>" }) - escaped dollar sign with an environment key in unquoted values will escape the
environment key rather than expand (
KEY=\$KEYbecomes{ KEY: "\\$KEY" }) - colon and a minus sign with a default value(which can also be another expand
value) in expanding construction in unquoted values will first attempt to
expand the environment key. If it’s not found, then it will return the default
value (
KEY=${KEY:-default}If KEY exists it becomes{ KEY: "<KEY_VALUE_FROM_ENV>" }If not, then it becomes{ KEY: "default" }. Also there is possible to do this caseKEY=${NO_SUCH_KEY:-${EXISTING_KEY:-default}}which becomes{ KEY: "<EXISTING_KEY_VALUE_FROM_ENV>" })
Add Package
deno add jsr:@std/dotenv
Import symbol
import * as dotenv from "@std/dotenv";
Import directly with a jsr specifier
import * as dotenv from "jsr:@std/dotenv";