http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Error: Cannot set headers after they are sent to client, occurs when trying to serve index.html, only occurs in v14.0.0

Open abhimonk opened this issue 4 years ago • 19 comments

Getting the following error when trying to go to the webpage returned by "npx http-server" serving my index.html:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

The issue does not occur if I use version 13.0.0

(See below for full stack trace)

If I repeat the reproduction steps with version 13.0.0, it works as expected.

Environment Versions

  1. OS Type: Windows
  2. Node version: 6.13.4
  3. http-server version: 14.0.0

Steps to reproduce

  1. Have just any index.html in your current folder
  2. Run "npx http-server" from the current folder
  3. Navigate to the URL returned (which should trigger serving your index.html)

Expected result

My index.html should be served and the contents of it should be displayed. This works fine in version 13.0.0.

Actual result

The server crashes with the following error:

[2021-10-23T17:10:29.869Z] "GET /" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36" (node:3956) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated _http_outgoing.js:485 throw new ERR_HTTP_HEADERS_SENT('set');

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:485:11) at module.exports.ResponseStream. [as setHeader] (C:\Users\abhim\Documents\GameDev\JSDev\AttackAnimationGenerator\node_modules\union\lib\response-stream.js:100:34) at Object.exports. (C:\Users\abhim\Documents\GameDev\JSDev\AttackAnimationGenerator\node_modules\http-server\lib\core\status-handlers.js:57:7)
at Readable. (C:\Users\abhim\Documents\GameDev\JSDev\AttackAnimationGenerator\node_modules\http-server\lib\core\index.js:339:22) at Readable.emit (events.js:210:5) at emitErrorNT (internal/streams/destroy.js:92:8) at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) code: 'ERR_HTTP_HEADERS_SENT' }

Other information

I have seen the github issue https://github.com/http-party/http-server/issues/634, but it looks like their server is undergoing external traffic. I am just using my server for local testing with a simple index.html. This workflow worked fine in version 13.0.0, but when I switched to version 14.0.0 I get the above error.

abhimonk avatar Oct 23 '21 17:10 abhimonk

same problem with docker node:12.10.0-alpine , now needed to set version [email protected] to fix this error

natali27 avatar Oct 25 '21 14:10 natali27

+1, using node v12.14.0

Ronkiro avatar Oct 29 '21 15:10 Ronkiro

Came here for this, v14 is broken. Need to use 13.0.2...

totszwai avatar Nov 02 '21 16:11 totszwai

Reproduced this with Node.js v12.14.0, but not with v12.22.7 (the latest release of v12.x). As a workaround, could you update to the v12.22.7 (or higher)?

boarwell avatar Nov 04 '21 04:11 boarwell

Same problem here with docker node:12.11.1-alpine

tutanck avatar Nov 04 '21 15:11 tutanck

Problem is not with docker(or container versions), i faced same problem on ubuntu. http-server@14 is broken, need install 13.0.2 to get things working.

wesleycsj avatar Nov 10 '21 18:11 wesleycsj

Reproduced this with Node.js v12.14.0, but not with v12.22.7 (the latest release of v12.x). As a workaround, could you update to the v12.22.7 (or higher)?

This makes this sound like it may be a bug in Node, can anyone else confirm newer versions of Node work with http-server v14?

thornjad avatar Dec 06 '21 20:12 thornjad

Seeing this on v12.13.1

matthias-ccri avatar Dec 08 '21 04:12 matthias-ccri

I'm seeing with v12.13.0

srlowe avatar Dec 13 '21 18:12 srlowe

Same here with v10.24.1

myleshk avatar Dec 20 '21 09:12 myleshk

I'm not currently able to reproduce this, tagging for help!

thornjad avatar Dec 20 '21 18:12 thornjad

Can reproduce on node v12.14.1

joshnoe avatar Dec 31 '21 19:12 joshnoe

My node is v10.19.0 and I'm getting the same error. I will uninstall and search for other options. Simple static http-server must be so simple, that things errors this never happens - regardless of node version. Suggestion: simplify the code radically.

systa avatar Jan 08 '22 12:01 systa

Can reproduce on node v10.19.0, I updated to latest stable node, 16.13.2 and it works fine now! https://phoenixnap.com/kb/update-node-js-version

ThomasDEVio avatar Jan 17 '22 20:01 ThomasDEVio

This is hiding another error

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type number
    at write_ (_http_outgoing.js:618:11)
    at ServerResponse.write (_http_outgoing.js:586:15)
    at module.exports.ResponseStream.write (/root/http-server/node_modules/union/lib/response-stream.js:186:22)
    at Readable.ondata (_stream_readable.js:722:22)
    at Readable.emit (events.js:209:13)
    at addChunk (_stream_readable.js:305:12)
    at readableAddChunk (_stream_readable.js:286:11)
    at Readable.push (_stream_readable.js:220:10)
    at next (_stream_readable.js:1218:27)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

and is happening since https://github.com/http-party/http-server/pull/736 @boarwell 's comment about failures on Node 10 probably applies to earlier versions of Node 12 too.

The issue can be reproduced with node (using alpine docker images) up to 12.15, it's OK in 12.16.

zbynek avatar Jan 23 '22 21:01 zbynek

I ran into this issue with and old AngularJS project on MacOS. I just installed http-server and it didn't work. Forcing to v13 made it work without changes.

[Tue Mar 29 2022 09:43:48 GMT+0200 (Central European Summer Time)]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
_http_outgoing.js:470
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at module.exports.ResponseStream.(anonymous function) [as setHeader] (/Users/barry/Sites/app/node_modules/union/lib/response-stream.js:100:34)
    at Object.exports.(anonymous function) (/Users/barry/Sites/app/node_modules/http-server/lib/core/status-handlers.js:57:7)
    at Readable.stream.on (/Users/barry/Sites/app/node_modules/http-server/lib/core/index.js:339:22)
    at Readable.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

barryvdh avatar Mar 29 '22 07:03 barryvdh

This issue has been inactive for 180 days

github-actions[bot] avatar Sep 25 '22 12:09 github-actions[bot]

bump, i think this still needs a solution

Ronkiro avatar Sep 30 '22 21:09 Ronkiro

Switching from node:10 to node:16 solved this issue for me.

KissBalazs avatar Apr 04 '23 16:04 KissBalazs