How to read environment variables in Deno

How to read environment variables in Deno

As Deno is gaining more popularity, demands for guidelines and documentation are booming. Deno has some documentations, but they are not the best and far from being comprehensive. That’s alright as it’s fairly new and is still evolving. Hence, I decided to take the liberty, and in the meanwhile dedicate some of my spare time to write tutorials on different Deno related topics. Today, I cover how to read environment variables in Deno.

There are two approaches to read environment variables in Deno,

  • With built-in Deno API
  • Using Dotenv library

Based on your preference, you may choose one over another. Each has some advantages and disadvantages that I explain later on.

With built-in Deno API

Similar to Node.js, Deno has a built-in feature to read environment variables. In Node.js we can read environment variables like this,

console.log(process.env.USERNAME);
console.log(process.env.PASSWORD);

In Deno, we can achieve the same as below,

console.log(Deno.env.get("USERNAME"));
console.log(Deno.env.get("PASSWORD"));

As you can see, it’s not much different from Node.js and very straightforward.

Important note: when you want to execute the code, you need to pass --allow-env flag, otherwise the code fails to run.

The advantages of this approach are:

  • No need for external library
  • Simple to use

The disadvantages are:

  • Need to explicitly set the flag
  • Variables can’t be externalized in a file
  • One has to export variables manually in advance

Using Dotenv library

Dotenv is a third-party library to work with environment variables in Deno. It inspired by Node dotenv module. Dotenv is not exactly used for reading system environment variables as its name implies. It’s mainly used to externalize configurations. Something that environment variables are widely used for.

To use dotenv first we need to create a .env file And then add needed variables there, instead of exporting them. For example,

# .env
USERNAME=test
PASSWORD=secret

Then we can read the variablesof the .env file with this code,

import { config } from "https://deno.land/x/dotenv/mod.ts";

console.log(config().USERNAME);
console.log(config().PASSWORD);

To run the code this time, we DO NOT need to set --allow-env flag anymore.

As stated before, with this approach, we can’t read the variables that are not set in the .env file. For example, we can’t read $USER that’s set in Unix/Linux. Additionally, this approach doesn’t export or overwrite environment variables system-wide (by default).

But that’s not all of it. If we really want to set a system environment variable, we can achieve that using auto loading feature as follow,

# .env
USERNAME=test
PASSWORD=secret
import { config } from "https://deno.land/x/dotenv/mod.ts";
import "https://deno.land/x/dotenv/load.ts";

console.log(Deno.env.get("USERNAME")); 
console.log(Deno.env.get("PASSWORD"));

And then run the code with the --allow-env flag.

In the above approach instead of mod.ts, we used load.ts that takes care of exporting variables to the OS.

Advantages of this approach are:

  • Perfect for externalizing configurations
  • A single file without the need to manually export variables
  • OS independent
  • Autoloading
  • Flexible

Disadvantages of this approach is:

  • An extra dependency on the project

If you are looking for more Deno related articles, don’t forget to check the Deno category of the website.

Inline/featured images credits