How to read environment variables in Deno using JavaScript

Deno's API for accessing environment variables is different than Node.js's API.

The way to read environment variables in Deno has changed from its early incarnation. Originally, there was a Deno.env() function that returned an object containing all of the defined environment variables. For v1.0.0, Deno switched to a Deno.env object with a get() method.

First, in order to read environment variables, you must run your code with the --allow-env flag, such as:

$ deno run --allow-env index.js

Then, inside your JavaScript, you can read an environment variable by passing a string to Deno.env.get(), such as:

const PORT = Deno.env.get("PORT");

You can also retrieve an object whose properties are all of the environment variables, similar to process.env in Node.js:

const env = Deno.env.toObject();

const PORT = env.PORT:

The Deno.env object also has set() and delete() methods that let you change the contents of the Deno.env object. You probably don’t want to use those methods regularly, however, they can be helpful for debugging purposes.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.