Skip to content

dotenv

The dotenv package reads KEY=VALUE files, conventionally named .env, and loads them into the environment so the rest of the program can read them with env::get.

dotenv::load() # reads ./.env
port = env::get("PORT")
dotenv::load("config/.env.production", false) # don't overwrite existing variables

load returns the values it read. parse_file and parse return the values without touching the environment:

settings = dotenv::parse("PORT=8080\nNAME=\"kiwi app\" # display name\n# a comment")
println settings # {"PORT": "8080", "NAME": "kiwi app"}

Values are always strings.

FormExampleNotes
PlainKEY=valueSurrounding whitespace is trimmed.
Double quotedKEY="hello world"Keeps whitespace; supports \", \\, \n, \t and \r.
Single quotedKEY='literal $value'Taken literally.
Inline commentKEY=value # noteEverything from # on is ignored.
export prefixexport KEY=valueThe export is ignored.
Comment# commentIgnored, as are blank lines.
dotenv::parse(content: string): hashmap

Parses a .env-formatted string into a hashmap.

ParameterTypeDefaultDescription
contentstringThe dotenv-formatted string to parse.

Returns hashmap — A hashmap of key-value pairs.

dotenv::parse_file(file_path: string = ".env"): hashmap

Parses a .env file into a hashmap without modifying the environment.

ParameterTypeDefaultDescription
file_pathstring".env"The path to the .env file. Defaults to ".env".

Returns hashmap — A hashmap of key-value pairs.

Throws string — If the file does not exist.

dotenv::load(file_path: string = ".env", overwrite: boolean = true): hashmap

Loads a .env file and sets each entry as an environment variable.

ParameterTypeDefaultDescription
file_pathstring".env"The path to the .env file. Defaults to ".env".
overwritebooleantrueWhen true, existing environment variables are overwritten. Defaults to true.

Returns hashmap — A hashmap of the loaded key-value pairs.

Throws string — If the file does not exist.