Loading dotENV files with Deno
Deno is a server runtime for javascript and typescript, it serves the same function as NodeJS, but different implementation. Deno strives to closely model the implementation of the browser version of javascript for better compatibility.
dotenv
One of the handiest modules in the NodeJS ecosystem is the dotenv module. With this module you can create a .env
file and place your environment variables for your application, then you can load them into your application using a require('dotenv').config()
function. In Deno, we do not have require, and Deno handles the loading of environment variables differently.
Deno.env
With Deno, you call, instead of the NodeJS way of process.env
being a simple object. The Deno.env
is not a plain object. You can access the values using the get
method.
dotenv for Deno
There is a dotenv for Deno, that is even easier to use than dotenv of NodeJS.
create your .env
GREETING=Hello
import the load.ts
module from the dotenv
package
import "https://deno.land/x/dotenv/load.ts";
console.log(Deno.env.get('GREETING'));
And now you can access your config information from a .env
just like you would access environment variables in Deno.
Summary
Deno has some different updated technology that still takes getting used to, but many libraries that we use in NodeJS have alternate libraries in Deno now and can serve the same purposes. Check out the Deno Third Party ecosystem for these implementations.
About hyper
hyper is a service framework built using Deno, check hyper out at https://hyper.io, learn how you can build scalable applications using hyper.