Environment Variables in Deno

 by Robin Wieruch
 - Edit this Post

Environment variables are great for hiding sensitive information about your Deno application. This can be API keys, passwords, or other data which shouldn't be visible to others. That's why there exists the .env file, which you would have to create, to hide sensitive information. We will create this file and pass some sensitive information to it:

PASSWORD=Geheimnis

In your source code files, you can use this environment variable with the dotenv third party library:

import { config } from 'https://deno.land/x/dotenv/mod.ts';
const password = config()['PASSWORD'];
console.log(password);
// "Geheimnis"

The utility function returns an object with all the key/value pairs from the .env file. Now the information isn't exposed in the source code anymore, but only available in the environment variables file.

Once you start your Deno application, you should see a permission error showing up on the command line: "Uncaught PermissionDenied: read access to "/Users/mydspr/Developer/Repos/deno-example", run again with the --allow-read flag". You can allow the access on environment variables with a permission flag in Deno:

deno run --allow-read index.ts

It's important to note that the .env file shouldn't be shared in a public repository where everybody can see it. If you make your source code public, for example on GitHub, consider adding the .env file to a .gitignore file.

Keep reading about 

In this brief walkthrough, we will use Babel Module Resolver to convert relative paths to aliases for your entire JavaScript application. You can install it via the command line: And use it in your…

Just recently I had to use Docker for my create-react-app web application development. Here I want to give you a brief walkthrough on how to achieve it. First of all, we need a React application…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.