Skip to content

Latest commit

History

History

dotenv

Folders and files

NameName
Last commit message
Last commit date

parent directory

..

cool/dotenv helps you load configurations from .env.* files and environment variables, a practice based on The 12-Factor App methodology which recommends separating configuration from code.

馃殌 Getting Started with Environment Variables (ENV)

Environment variables (often shortened to env vars or env) are variables available in all command line sessions and affect the behavior of the applications on your system. They are essential for managing the configuration of your applications separate from your code.

The 12-Factor App methodology recommends storing configurations in environment variables.

The 12-Factor App

The 12-Factor App is a set of best practices designed to enable applications to be built with portability and resilience when deployed to the web. Learn more about The 12-Factor App.

馃 What cool/dotenv offers?

cool/dotenv helps you handle these configurations properly. With using cool/dotenv, the environment variables are loaded from the following sources:

  • Environment variables that passed to the Deno process.
  • Environment variables defined by the system's shell.
  • .env.$(ENV).local file - Local overrides of environment-specific settings.
  • .env.local file - Local overrides. This file is loaded for all environments except "test".
  • .env.$(ENV) file - Environment-specific settings.
  • .env file - The default configuration file.

These files are loaded in the order listed above. The first value set (either from file or environment variable) takes precedence. That means you can use the .env file to store default values for all environments and .env.development.local to override them for development.

馃洜 Usage and API Reference

Here you'll find a list of features provided by cool/dotenv along with brief descriptions and usage examples.

Loading environment variables

Basic usage:

import { load } from "$cool/dotenv/mod.ts";

const vars = await load();
console.log(vars);

Load from different directory:

import { load } from "$cool/dotenv/mod.ts";

const vars = await load({ baseDir: "./config" });
console.log(vars);

Configuring an options object with environment reader

Basic usage:

import { configure, env } from "$cool/dotenv/mod.ts";

const options = await configure(
  (reader, acc) => {
    acc["env"] = reader[env];
    acc["port"] = reader.readInt("PORT", 8080);

    return acc;
  },
  {},
);

With custom interfaces:

import { configure, env } from "$cool/dotenv/mod.ts";

type Options = {
  env: string;
  port: number;
};

const options = await configure<Options>(
  (reader, acc) => {
    acc.env = reader[env];
    acc.port = reader.readInt("PORT", 8080);

    return acc;
  },
  {},
);

馃敆 For further details such as requirements, licensing and support guide, please visit the main cool repository.