# DotenvParser - A simple library to parse .env files

**URL:** <https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009>\
**Category:** Announcing\
**Tags:** library, environment, configuration, environment-variables, dev-environment, dotenv, dotenv\_parser\
**Created:** [January 22, 2021, 6:32am UTC](https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009 "2021-01-22T06:32:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nicd/32/2357_2.png) [@Nicd](https://forum.elixirforum.com/u/Nicd)\
**Post date:** [January 22, 2021, 6:32am UTC](https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009/1 "2021-01-22T06:32:13Z")

</div>

I was refactoring my project to use [`config/runtime.exs`](https://gitlab.com/code-stats/code-stats/-/blob/master/config/runtime.exs) to make the config more streamlined and allow configuring (nearly) all things with environment variables. This is fine for production where I define the variables in the Systemd unit file, but in development I don’t want to waste time having to set all of them or remembering to source them from somewhere.

So I decided to write a simple .env file parsing library so I can do this on top of my config file:

```elixir
if Config.config_env() == :dev do
  DotenvParser.load_file(".env")
end

```

Now for developers, all the vars are nicely grabbed from the .env and they can be used in the runtime config (or inside the application code) with `System.get_env/2`. When execution ends, they are no longer set, so they don’t pollute your shell environment.

The library supports both uppercase and lowercase variable names, quoted values to prevent trimming of whitespace, comment lines and inline comments, double quoted values to unescape some escape codes like `\n` or `\uXXXX`, and lines that start with `export` (in case you want shell compatibility – but note that the escapes may be different).

Link to library: [dotenv\_parser | Hex](https://hex.pm/packages/dotenv_parser)

License: MIT (+ BSD 3 clause for one test file)

---

<div class="post-metadata">

**Author:** ![Nicd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nicd/32/2357_2.png) [@Nicd](https://forum.elixirforum.com/u/Nicd)\
**Post date:** [January 22, 2021, 6:55am UTC](https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009/2 "2021-01-22T06:55:32Z")

</div>

Some prior art, some of them I looked through and some I didn’t know of before this moment:

[dotenv](https://hex.pm/packages/dotenv) – I figured it was abandoned but in fact at pretty much the same time as I made my own, it has reactivated and pushed a new version. 😃 Has a server mode with a reload function to reload values when they have been changed. Supports referring to previous variables in subsequent ones, which mine doesn’t. Doesn’t seem to have any unescape support?

[envy](https://hex.pm/packages/envy) – Puts values directly to system env (no possibility to return them). Doesn’t do unescaping. Has a Mix-based config reload system.

[stillir](https://hex.pm/packages/stillir) – In Erlang, I don’t know how to use it.

[exenv](https://hex.pm/packages/exenv) – Has different adapters and encryption support.

---

<div class="post-metadata">

**Author:** ![Nicd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nicd/32/2357_2.png) [@Nicd](https://forum.elixirforum.com/u/Nicd)\
**Post date:** [January 22, 2021, 6:57am UTC](https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009/3 "2021-01-22T06:57:51Z")

</div>

## Serving suggestion

Since environment variables are always strings, so I wrote this helper function to grab variables and cast them to the correct type: [lib/code\_stats\_web/config\_helpers.ex · d189c46930d1c352af8ca63027d56b346814ab32 · CodeStats / code-stats · GitLab](https://gitlab.com/code-stats/code-stats/-/blob/d189c46930d1c352af8ca63027d56b346814ab32/lib/code_stats_web/config_helpers.ex)

You can see it in action in the aforementioned [config/runtime.exs](https://gitlab.com/code-stats/code-stats/-/blob/master/config/runtime.exs) file.

---

<div class="post-metadata">

**Author:** ![Nicd](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nicd/32/2357_2.png) [@Nicd](https://forum.elixirforum.com/u/Nicd)\
**Post date:** [January 28, 2022, 6:19pm UTC](https://forum.elixirforum.com/t/dotenvparser-a-simple-library-to-parse-env-files/37009/4 "2022-01-28T18:19:44Z")

</div>

Thanks to a suggestion by @axelson, DotenvParser 2.0.0 now supports multiline environment variables. To see all supported formats, it’s probably easiest to look at the test file: [test/data/.env · 735e1867f69d94da8abb68ad283a766a7c2efa56 · Mikko Ahlroth / dotenv-parser · GitLab](https://gitlab.com/Nicd/dotenv-parser/-/blob/735e1867f69d94da8abb68ad283a766a7c2efa56/test/data/.env) (though the documentation lists supported escapes in double quoted env vars).
