Dotenv

Dotenv is a zero-dependency module that loads environment variables from a .env file into Deno.env, inspired by motdotla/dotenv.

Usage

Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
import { config } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

const result = config();

if (result.error) {
  throw result.error;
}

console.log(result.parsed);

Auto loading

load.ts automatically loads environment variables from a .env file into Deno.env:

# .env
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
import "https://raw.githubusercontent.com/daychongyang/dotenv/master/load.ts";

console.log(Deno.env.get("DB_HOST")); // localhost
console.log(Deno.env.get("DB_USER")); // root
console.log(Deno.env.get("DB_PASS")); // s1mpl3

Config

config will read your .env file, parse the contents, and return an Object with a parsed key containing the loaded content or an error key if it failed.

You can additionally, pass options to config.

Options

Path

Default: resolve(Deno.cwd(), '.env')

You may specify a custom path if your file containing environment variables is located elsewhere.

import { config } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

config({ path: "/custom/path/to/.env" });
Encoding

Default: utf-8

You may specify the encoding of your file containing environment variables.

import { config } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

config({ encoding: "utf-8" });
Debug

Default: false

You may turn on logging to help debug why certain keys or values are not being set as you expect.

import { config } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

config({ debug: true });

Parse

The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.

import { parse } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

const env = parse(`DB_HOST=localhost`); // will return an object
console.log(typeof env, env); // object { DB_HOST : 'localhost' }

Options

Debug

Default: false

You may turn on logging to help debug why certain keys or values are not being set as you expect.

import { parse } from "https://raw.githubusercontent.com/daychongyang/dotenv/master/mod.ts";

const config = dotenv.parse("hello world", { debug: true });
// expect a debug message because the buffer is not in KEY=VAL form