Skip to main content
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 loads environment variables from a .env file into the current process, or stringify data into a .env file format.

Note: The key needs to match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/.

// Automatically load environment variables from a `.env` file
import "@std/dotenv/load";
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'");

Add to your project Jump to heading

deno add jsr:@std/dotenv

See all symbols in @std/dotenv on

What is dotenv? Jump to heading

dotenv is a popular way to manage environment variables in development. It loads key-value pairs from a .env file into process.env, making it easy to configure your app without hardcoding values.

When to use @std/dotenv Jump to heading

Use this package to load local configuration during development, for preview builds, or in simple deployments where your platform doesn’t inject environment variables for you, keeping secrets and config out of source code.

Examples Jump to heading

// Auto-load from .env at process start
import "@std/dotenv/load";

// Or load manually
import { load } from "@std/dotenv";

const env = await load({
  envPath: ".env.local", // where to read variables from (default: .env)
  defaultsPath: ".env.defaults", // optional defaults if a key is missing
  examplePath: ".env.example", // assert required keys exist
  export: true, // also export to the process environment
  allowEmptyValues: true, // allow empty values in example/defaults
  override: false, // do not overwrite existing env vars by default
});

// Values are available both from the returned object and (when export: true)
// via the runtime’s environment API (e.g., Deno.env)
console.log(env.DATABASE_URL);
// Deno.env.get("DATABASE_URL");
// Parse and stringify
import { parse, stringify } from "@std/dotenv";

const text = `
GREETING="hello world" # comment
PORT=8080
`;

parse(text); // { GREETING: "hello world", PORT: "8080" }

stringify({ GREETING: "hello world" });
// "GREETING='hello world'"

Tips Jump to heading

  • Don’t commit secrets. Add .env to your project's .gitignore file.

Did you find what you needed?

Privacy policy