Skip to content

ReferenceError: colors and logger warning are not defined when using --websocket flag #949

@HarshVz

Description

@HarshVz

Bug: colors is not defined when using --websocket flag

When attempting to use the --websocket flag without the --proxy flag, the server crashes with a ReferenceError: colors is not defined error.

Environment Versions

  1. OS Type: Windows 11
  2. Node version: v22.17.0 (or run node --version)
  3. http-server version: v14.1.2 (master branch, unreleased)

Steps to reproduce

  1. Clone the repository and install dependencies:

    git clone https://github.com/http-party/http-server.git
    cd http-server
    npm install
  2. Run http-server with the --websocket flag but without --proxy:

    node bin/http-server --websocket
  3. Observe the crash

Expected result

The server should start and display a warning message:

WebSocket proxy will not be enabled because proxy is not enabled

Then continue running normally.

Actual result

The server crashes immediately with:

C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:233
      logger.warning(colors.yellow('WebSocket proxy will not be enabled because proxy is not enabled'));
                     ^

ReferenceError: colors is not defined
    at listen (C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:233:22)
    at C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:185:5
    ...

also, warning is not defined as well for logger:

Image

Root Cause

There are two bugs on line 233 in bin/http-server:

  1. Undefined variable: The code references colors which is never imported. The file imports chalk instead (line 5).
  2. Undefined method: The code calls logger.warning() which doesn't exist. The logger object only has info and request methods (defined at lines 138-171).

Current code (line 233):

logger.warning(
  colors.yellow(
    "WebSocket proxy will not be enabled because proxy is not enabled",
  ),
);

Should be:

logger.info(
  chalk.yellow(
    "WebSocket proxy will not be enabled because proxy is not enabled",
  ),
);

Additional Bug: Same issue in error handler

There's also a similar bug in the nopanic error handler (lines 115-118) where colors is used instead of chalk:

console.log(colors.green(etime));
console.log(`${colors.red("Fatal error: ")}${e.code}: ${e.message}`);
console.log(colors.bold(`Check ${filename} file in this folder.`));

All three instances should use chalk instead of colors.

Other information

  • The bug is currently only in the master branch and has not been released to npm yet
  • This affects the unreleased websocket proxy feature
  • The fix is simple: replace colors with chalk and logger.warning with logger.info

i was going through the codebase- just curios on how it works and found it!

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