Skip to content

env variable DEBUG is not parsed correctly #989

Open

Description

@zymon

If the DEBUG env variable contains multiple items separated with spaces, then only the first one is used.

For example:
export DEBUG="APP_X APP_Y APP_Z"

Then, only the APP_X is enabled for debugging. Other apps are ignored. This is due to the way the string is parsed:

In debug\src\common.js, line 169:

const split = (typeof namespaces === 'string' ? namespaces : '') .trim() .replace(' ', ',') .split(',') .filter(Boolean);

replace('', ',') changes only the first occurrence of space

So, to fix it you can use regexp:

const split = (typeof namespaces === 'string' ? namespaces : '') .trim() .replace(/ /g, ',') .split(',') .filter(Boolean);

Activity

  1. jheeanny commented on Feb 17, 2026

    @jheeanny

    You identified the bug correctly. The replace(" ", ",") only replaces the first space. Changing it to replace(/\s/g, ","), or better replace(/\s+/g, ","), will split all whitespace-separated namespaces.

    This is a one-line fix in src/common.js around line 169.

    Suggestion: also add a unit test for multiple spaces (e.g., DEBUG="A B C" should enable all three). Could you open a PR? It is a safe, high-impact fix for many users.

  2. abhu85 commented on Feb 23, 2026

    @abhu85

    Good news - this bug has already been fixed!

    Looking at the current src/common.js (line 171), the code now uses:

    .replace(/\s+/g, ',')

    instead of the original .replace(' ', ',') which only replaced the first space.

    This fix is included in v4.4.3 (the latest release on npm). Users experiencing this issue should upgrade:

    npm install debug@latest

    I believe this issue can be closed.

  3. abhu85 commented on Mar 2, 2026

    @abhu85

    This issue appears to be fixed in the current codebase (v4.4.3).

    Current code at src/common.js:171:

    const split = (typeof namespaces === 'string' ? namespaces : '')
        .trim()
        .replace(/\s+/g, ',')  // Uses regex with global flag
        .split(',')
        .filter(Boolean);

    Verification:

    const debug = require('debug');
    debug.enable('APP_X APP_Y APP_Z');
    console.log(debug.names); // ['APP_X', 'APP_Y', 'APP_Z'] ✅

    All three namespaces are correctly parsed and enabled. The fix likely came in during the security updates in September 2025.

    This issue can probably be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions