deno.land / std@0.139.0 / dotenv
Setup a .env
file in the root of your project.
# .env
GREETING=hello world
Then import the configuration using the config
function.
// app.ts
import { config } from "https://deno.land/std@0.139.0/dotenv/mod.ts";
console.log(await config());
Then run your app.
> deno run --allow-read app.ts
{ GREETING: "hello world" }
path?: string
: Optional path to .env
file. Defaults to ./.env
.export?: boolean
: Set to true
to export all .env
variables to the
current processes environment. Variables are then accessable via
Deno.env.get(<key>)
. Defaults to false
.safe?: boolean
: Set to true
to ensure that all necessary environment
variables are defined after reading from .env
. It will read .env.example
to get the list of needed variables.example?: string
: Optional path to .env.example
file. Defaults to
./.env.example
.allowEmptyValues?: boolean
: Set to true
to allow required env variables to
be empty. Otherwise it will throw an error if any variable is empty. Defaults
to false
.defaults?: string
: Optional path to .env.defaults
file which defaults to
./.env.defaults
.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.139.0/dotenv/load.ts";
console.log(Deno.env.get("GREETING"));
> deno run --allow-env --allow-read app.ts
hello world
To enable safe mode, create a .env.example
file in the root of the project.
# .env.example
GREETING=
Then import the configuration with safe
option set to true
.
// app.ts
import { config } from "https://deno.land/std@0.139.0/dotenv/mod.ts";
console.log(await config({ safe: true }));
If any of the defined variables is not in .env
, an error will occur. This
method is preferred because it prevents runtime errors in a production
application due to improper configuration.
Another way to supply required variables is externally, like so:
GREETING="hello world" deno run --allow-env app.ts
Default values can be easily added via creating a .env.defaults
file and using
the same format as an.env
file.
# .env.defaults
# Will not be set if GREETING is set in base .env file
GREETING="a secret to everybody"
The parsing engine currently supports the following rules:
export: true
BASIC=basic
becomes { BASIC: "basic" }
#
are treated as commentsEMPTY=
becomes { EMPTY: "" }
)SINGLE_QUOTE='quoted'
becomes
{ SINGLE_QUOTE: "quoted" }
)MULTILINE="new\nline"
becomes{ MULTILINE: "new
line" }
JSON={"foo": "bar"}
becomes
{ JSON: "{\"foo\": \"bar\"}" }
)trim
)
(FOO= some value
becomes { FOO: "some value" }
)FOO=" some value "
becomes { FOO: " some value " }
)dotenv
.Version Info