question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Best Practice] Boolean Values in Config Files

See original GitHub issue

Dear all,

thanks for this wonderful package. I recently stumbled upon an issue where i would like to get your input on how to deal with this properly (e.g., best practice).

Consider the following config file:

// config/debug.ts
export default {
  debug_mode: process.env.APP_DEBUG || false,
};

Basically, it reads the .env file to get the APP_DEBUG config value, otherwise it is set to false. Now, if i want to use it like this in a service:

if (configService.get('debug.debug_mode', false) === true) {
   // do something that needs to be done, if the debug mode is enabled, e.g., extensive logging
}

this is never executed, because the value, that is read from the .env file is interpreted as string (e.g., typeof configService.get('debug.debug_mode') is string and not boolean as one would guess.

How do you deal with boolean values in .env files specifically? For now, the best approach for me is as follows

// "cast" it as boolean
const enabled = !! configService.get('debug.debug_mode', false);
if (enabled === true) { /* ... */ }

What would you suggest? All the best

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
michaelyalicommented, Jan 8, 2019

Hi guys! Let me share my approach if you don’t mind 😃

import { get } from 'env-var';

export default {
  version: get('SERVER_VERSION', 'v1').asString(),
  port: get('SERVER_PORT', '3000').asIntPositive(),
};

Cheers!

3reactions
johannesschobelcommented, Jan 8, 2019

i just tried the env-var approach and it works like a charm! I really (!) like it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set boolean values in an INI configuration file?
I've seen a variety of ways used to set boolean values in INI files: variable = ...
Read more >
How to Write a Configuration file in Python | by Xiaoxu Gao
In this article, I want to share with you some good practices of ... one is Boolean type as it's able to recognize...
Read more >
Guidelines for changing configuration files - IBM
General guidelines. Use the following general guidelines when you change the configuration settings: ; Default values. Use the following guidelines when changing ...
Read more >
What is a best practice to represent a boolean value in a shell ...
In general when a script interprets an environment variable to be either true or false, it will interpret any value at all to...
Read more >
Tips on naming boolean variables - Cleaner Code
Boolean that verifies that every case is true · Boolean that verifies that one of many cases is true · Avoid custom prefixes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found