Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/dotenv/mod.ts

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
import * as mod from "https://deno.land/std@0.192.0/dotenv/mod.ts";

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 "https://deno.land/std@0.192.0/dotenv/mod.ts";

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 "https://deno.land/std@0.192.0/dotenv/load.ts";

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=basic becomes { 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 value becomes { 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=$KEY or KEY=${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=\$KEY becomes { 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 case KEY=${NO_SUCH_KEY:-${EXISTING_KEY:-default}} which becomes { KEY: "<EXISTING_KEY_VALUE_FROM_ENV>" })