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
- OS Type: Windows 11
- Node version:
v22.17.0 (or run node --version)
- http-server version:
v14.1.2 (master branch, unreleased)
Steps to reproduce
-
Clone the repository and install dependencies:
git clone https://github.com/http-party/http-server.git
cd http-server
npm install
-
Run http-server with the --websocket flag but without --proxy:
node bin/http-server --websocket
-
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:
Root Cause
There are two bugs on line 233 in bin/http-server:
- Undefined variable: The code references
colors which is never imported. The file imports chalk instead (line 5).
- 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!
Bug:
colors is not definedwhen using--websocketflagWhen attempting to use the
--websocketflag without the--proxyflag, the server crashes with aReferenceError: colors is not definederror.Environment Versions
v22.17.0(or runnode --version)v14.1.2(master branch, unreleased)Steps to reproduce
Clone the repository and install dependencies:
git clone https://github.com/http-party/http-server.git cd http-server npm installRun http-server with the
--websocketflag but without--proxy:Observe the crash
Expected result
The server should start and display a warning message:
Then continue running normally.
Actual result
The server crashes immediately with:
also, warning is not defined as well for logger:
Root Cause
There are two bugs on line 233 in
bin/http-server:colorswhich is never imported. The file importschalkinstead (line 5).logger.warning()which doesn't exist. The logger object only hasinfoandrequestmethods (defined at lines 138-171).Current code (line 233):
Should be:
Additional Bug: Same issue in error handler
There's also a similar bug in the
nopanicerror handler (lines 115-118) wherecolorsis used instead ofchalk:All three instances should use
chalkinstead ofcolors.Other information
masterbranch and has not been released to npm yetcolorswithchalkandlogger.warningwithlogger.infoi was going through the codebase- just curios on how it works and found it!