Deno is a small, general-purpose runtime environment for JavaScript with first class support for various interfaces, ECMAScript modules, and seamless TypeScript integration.
Environment variables are one of the best features of Deno. They are used to hide sensitive information such as passwords, keys, usernames, and restricting users by limiting public access to this information. This information is stored in the .env
created by the user to hide it. Look at the example below:
MY_PASSWORD = Hello00001234
In your source code files, declare the third party dotenv
library and import config
from it. Look at the code below:
import { config } from 'https://deno.land/x/dotenv/mod.ts';
const pswd = config()['MY_PASSWORD'];
console.log(pswd);
// "Hello00001234"
This utility function will return an object with all the key/value pairs from the .env
file. This information is not accessed by the source code anymore and is only available in this .env
file.
Once the Deno application is started, you will see a permission error showing up in the terminal. You can remove this error by allowing the access of environment variables with the permission flag. Look at the command below:
deno run --allow-read deno_index.ts
// deno_index is the name of our created file.
It is vital to understand that
.env
is not shared in public mode for everyone to access. The entire reason for using this platform is to hide this sensitive information from the public.
RELATED TAGS
View all Courses