Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 usedotenv files. They are great for storing app configs:everything that is likely to vary between deploys

MY_APP_KEY=supersecret
Enter fullscreen modeExit 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 thatevery value is a string

DAYS_UNTIL_DATA_DELETION=60AWS_DISABLE=true
Enter fullscreen modeExit fullscreen mode

Both are in factstrings. 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 modeExit 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 modeExit 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 byTekton onUnsplash

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
tshammer profile image
tshammer
Former jQuery expert learning Vue and making awesome projects
  • Education
    High school
  • Work
    Lead app developer
  • Joined

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.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

11+ years of experience tangled in the world wide web.
  • Location
    Europe
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp