DEV Community

Cover image for A simple trick for your dotenv files
Victor Ocnarescu
Victor Ocnarescu

Posted on

A simple trick for your dotenv files

I'm sure most of you know and use dotenv files. They are great for storing app configs: everything that is likely to vary between deploys

MY_APP_KEY=supersecret
Enter fullscreen mode Exit fullscreen mode

These app configs are stored in a convenient key/value pair in a dotenv file. But what may come as a surprise to you is that every value is a string

DAYS_UNTIL_DATA_DELETION=60
AWS_DISABLE=true
Enter fullscreen mode Exit fullscreen mode

Both are in fact strings. Later on, when you will want to check if AWS is disabled, you will have to write this code (which I have seen over and over again in many projects)

if (process.env.AWS_DISABLE === "true") { ... } // what???
Enter fullscreen mode Exit fullscreen mode

That is just silly! When faced with boolean values I always write them with yes/no instead of true/false.

Furthermore, I quote every value in the dotenv files to make sure that every developer that reads the file knows the values are strings.

Here is the result:

MY_APP_KEY="supersecret"
DAYS_UNTIL_DATA_DELETION="60"
AWS_DISABLE="yes"
Enter fullscreen mode Exit fullscreen mode

Hope you enjoyed reading the article as much as I enjoyed writing it. And please do not store your boolean config values as strings. Give the yes/no option a try.

Photo by Tekton on Unsplash

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (1)

Collapse
 
tshammer profile image
tshammer

What I do is define an array of false-ish strings then use a ternary operator to convert the .env variable value to a boolean

const falsey = [ "false", "no", "0", "disabled", "disable" ]
dotenv.config()
const DEBUG=process.env.DEBUG ? (falsey.includes(process.env.DEBUG) ? false:true) : false

Nice and concise with few lines of code.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs