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.

Does 'debug' cache the DEBUG environment variable somewhere? Getting no output after changing namespace and DEBUG env var.

See original GitHub issue

I am using ‘debug’ with TypeScript in an Electron project like so:

import debug from 'debug';
const log = debug('foo');

export default async function myFunction(name: string) {
  log('calling myFunction');
  ...
  log('successfully returning myFunction');
  return someVariable;
}

in my shell (zsh) I set the DEBUG env var like so export DEBUG=foo and then started running my application. Everything worked fine. Eventually, I changed the namespace from foo to bar and now no matter what I do, I cannot get any output. I tried resetting DEBUG, I restarted my shell and set the environment variable again with export DEBUG=bar, I tried using the [cross-env]{https://www.npmjs.com/package/cross-env} to set it like so:

"scripts": {
    "start": "tsc && cross-env DEBUG=bar NODE_ENV=development electron-forge start",
    ...
}

but nothing ever shows up. I tried

import debug from 'debug';
const log = debug('bar');

console.log("DEBUG", process.env.DEBUG);

export default async function myFunction(name: string) {
  log('calling myFunction');
  ...
  log('successfully returning myFunction');
  return someVariable;
}

and the console.log output is indeed: DEBUG bar.

The only way I can get it to work now is by doing:

import debug from 'debug';
const log = debug('bar');
log.enabled = true;

The moment I change const log = debug('bar'); back to const log = debug('foo'); , I get output messages for the foo namespace no matter what’s in the DEBUG env var.

Any idea what might be happening here?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
algernoncommented, Jul 26, 2022

I’ve been bitten by this the other day too. Turns out, DEBUG is cached in LocalStorage, and in subsequent runs, LocalStorage will override the environment. You can do localStorage.debug = null in a dev console to reset it.

0reactions
GodBleakcommented, Sep 18, 2022

Are/were you using regex in your code somewhere? possibly passing it into debug? I think this issue was introduced with toNamespace() in 4.1.0

When I had DEBUG = *, I saw express was able to debug fine. I noticed that they’re using debug 2.6.9, so I went through the versions and found that debug works as expected until 4.1.0. Seeing that toNamespace converts regex into a namespace and realizing I was potentially passing regex into debug, I remembered Javascript’s regex is weird. commenting out my regex returned the output in 4.3.4.

However, at that point, the universe decided I was done, as uncommenting it did not reintroduce the issue and no matter what I do now I can’t reproduce it again, I’ve rolled everything back to exactly as it was when I had the problem and it’s just gone 🤷

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · debug-js/debug - GitHub
Does 'debug' cache the DEBUG environment variable somewhere? Getting no output after changing namespace and DEBUG env var. ... Possibility to read in...
Read more >
Environment variables in Compose | Docker Documentation
When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in....
Read more >
How do I set specific environment variables when debugging ...
Open your project. Go to Project -> Properties... Under Configuration Properties -> Debugging, edit the 'Environment' value to set environment ...
Read more >
Introduction to environment variables - CircleCI
Secrets masking is applied to environment variables set within Project Settings or Contexts in the web app. The value of the environment variable...
Read more >
Configuration | NestJS - A progressive Node.js framework
Since configuration variables change, best practice is to store ... defined environment variables are visible inside Node.js through the process.env global.
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