Angular Icon Get 67% off the Angular Master Bundle!

See the bundle then add to cart and your discount is applied.

0 days
00 hours
00 mins
00 secs
Deno

Handling Environment Variables in Deno

In this post you’ll learn how to use environment variables (.env) in Deno and how they differ from Node.js environment variables.

If you’ve also used Node.js before, this post will show you how to do it the Deno way.

What is the .env file?

The .env file is for keeping all your secrets, that means anything from a password to private or public API keys and more.

Typically a .env looks something like this:

FIREBASE_API_KEY=XYZDEF123987...
STRIPE_API_KEY=56780987ABCQWERTY...

🍭 The .env file lives in the root of your project and should not be checked into version control - locally create it and add it to your .gitignore!

.env in Node.js

Coming from a Node.js background, we’d then expect to access our variables such as FIREBASE_API_KEY from the magical process.env variable:

import { config } from 'dotenv';
config();

const firebaseKey = process.env.FIREBASE_API_KEY;
const stripeKey = process.env.STRIPE_API_KEY;

To supply custom environment variables we’d either use an inline script such as NODE_ENV=dev node app.js or the dotenv package and call config().

They’d then be available as process.env.<VARIABLE>.

.env in Deno

In Deno, it feels a bit nicer as we can import config and simply call it:

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

const env = config();

console.log(env.FIREBASE_API_KEY); // 'XYZDEF123987...'
console.log(env.STRIPE_API_KEY); // '56780987ABCQWERTY...'

Our env variable contains the value returned from the config() call - an object with our variables as keys 🔑.

Or better yet with ES6 object destructuring:

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

const { FIREBASE_API_KEY, STRIPE_API_KEY } = config();

console.log(FIREBASE_API_KEY); // 'XYZDEF123987...'
console.log(STRIPE_API_KEY); // '56780987ABCQWERTY...'

We also benefit from not having a node_modules folder anymore, with Deno all dependencies are hosted elsewhere and not locally. This will surely make a lot of developers happier and reduces the complexities dealing with local dependencies - especially across projects and teams.

It’s recommended by the Deno authors to use Safe Mode in Deno. Alongside our existing code, here’s how we can add it:

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

const { FIREBASE_API_KEY, STRIPE_API_KEY } = config({ safe: true });

console.log(FIREBASE_API_KEY); // 'XYZDEF123987...'
console.log(STRIPE_API_KEY); // '56780987ABCQWERTY...'

Safe mode in Deno will produce an error if an environment variable is missing, we don’t want our entire app burning in production do we? Enable it!

🕵️‍♀️ Check out the full documentation on Environment Variables in Deno which contains more information on additional options you can use.

JavaScript Array Methods eBook Cover

🎉 Download it free!

Ready to go beyond ForEach? Get confident with advanced methods - Reduce, Find, Filter, Every, Some and Map.

  • Green Tick Icon Fully understand how to manage JavaScript Data Structures with immutable operations
  • Green Tick Icon 31 pages of deep-dive syntax, real-world examples, tips and tricks
  • Green Tick Icon Write cleaner and better-structured programming logic within 3 hours

As an extra bonus, we'll also send you some extra goodies across a few extra emails.

This was a quick getting started post for those interested in learning Deno coming from a Node.js background on how to setup your environment variables using the .env file in Deno!

Happy coding!

Free eBooks:

JavaScript Array Methods eBook Cover

Ready to go beyond ForEach? Get confident with advanced methods - Reduce, Find, Filter, Every, Some and Map.

NestJS Build a RESTful CRUD API eBook Cover

Build your first NestJS app. With the CLI you'll learn the basics of real-world NestJS development.