1

In the documentation for visionmedia's debug module for Node.js, I see that it is possible to enable and disable different debug targets programmatically.

My attempts to do this have not been very successful.

I created a barebones script: test.js

const debug = require("debug");
let test = debug("test"); // created before being enabled

console.log(1, debug.enabled("test"));

let namespaces = debug.enable("test");
// test = debug("test");
console.log(2, debug.enabled("test"));
console.log("enabled namespaces:", namespaces);

test("this is test");

namespaces = debug.disable();
// test = debug("test");
console.log(3, debug.enabled("test"));
console.log("disabled namespaces:", namespaces);

test("you shouldn't see this");

When I run the script using node test.js, I get the following output, which shows that debug.enable("test") has neither enabled the test debug target, nor has it returned any namespaces:

1 false
2 true
enabled namespaces: undefined
3 false
disabled namespaces: undefined

When I run the script using DEBUG=test node test.js, I get the following output, which shows that debug.disable("test") has neither disabled the test debug target, nor has it returned the names of any disabled namespaces:

1 true
2 true
enabled namespaces: undefined
  test this is test +0ms
3 false
disabled namespaces: undefined
  test you shouldn't see this +0ms

However, if I uncomment the lines that recreate the test debug target after using .enable() or .disable(), then everything works.

It seems that, in order to be able to enable and disable debug targets programatically, I need to use a different syntax:

debug("test")("message which can be enabled or disabled");

But it seems that this creates a new debug instance each time it is used, which would be wasteful. And it is not the standard syntax described in the documentation. And, even so, the .enabled() and .disabled() methods do not return a list of namespaces.

Is there something that I have badly misunderstood?

Progman
  • 16,827
  • 6
  • 33
  • 48
James Newton
  • 6,623
  • 8
  • 49
  • 113

1 Answers1

1

The issue was caused because I was relying a version of debug installed as a dependency for express. It turns out that the current version of express requires an outdated version of debug.

After running npm install express today (27 June 2021), the entry for debug in package-lock.json was as follows.

"debug": {
  "version": "2.6.9",
  "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
  "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
  "requires": {
    "ms": "2.0.0"
  }

"version": "2.6.9"

(This is version "4.17.1" of express.)

This is because the package.json file for express explicitly calls for "debug": "2.6.9".

Running npm update has no effect on this.

Running npm install debug does fix the problem. The entry for debug in package-lock.json gets set to the following:

"debug": {
  "version": "4.3.1",
  "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz",
  "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==",
  "requires": {
    "ms": "2.1.2"
  }

"version": "4.3.1"

Problem solved.

James Newton
  • 6,623
  • 8
  • 49
  • 113