Urgent.News

What's breaking now, across thousands of outlets.

Tech

DEBUG=false but my app still runs in debug mode: why an environment variable "false" is true

You put DEBUG=false in .env , restart, and the app is still in debug mode. If you have searched "environment variable false is true" or "os.getenv boolean", here is the whole explanation: an environment variable is always a string, and "false" is a non-empty string, so it is truthy. Python's os.getenv , Node's process.env and Vite's import.meta.env all hand you text, never a bool or a number.…

Despite setting the DEBUG variable to false in a .env file and restarting the application, it still operates in debug mode. This perplexing issue is due to the nature of environment variables, which always consist of string values. In programming languages like Python, Node, and Vite, environment variables are returned as text, not as booleans or numbers.

Consequently, the false value, being a non-empty string, is considered truthy. When you attempt to convert an environment variable to a boolean using os.getenv or process.env in Node, the result is always true for any variable set to "false". Similarly, using int(os.getenv) or JSON.parse(process.env.X) leads to errors when the variable is missing.

To resolve this, read the environment variable as a string and explicitly convert it to the desired type when needed. For instance, setting DEBUG = os.getenv('DEBUG', '').strip().lower() in ('1', 'true', 'yes', 'on') ensures that the variable is treated as a string and only converted to a boolean if explicitly set to one of the specified truthy values.

Similarly, converting WORKERS to an integer with int(os.getenv('WORKERS', '4')) provides a default value of 4 when the variable is unset. By following these guidelines and treating only flags as parsed values, you can avoid the common pitfalls associated with environment variables.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Friday 2 October →