Netlify DEV - Randomly getting TypeError: res.writeHead is not a function at ProxyServer

Hi,
out of nowhere I’m getting this weird error on localhost running netlify dev… it’s happening randomly, works for some time, then I’m getting this error and nothing works… tried reinstalling nodeJS, NPM and tried various versions… currently running node 16.17.0

I don’t know how to debug this, any help?

/Users/peteroravec/.nvm/versions/node/v16.17.0/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^

TypeError: res.writeHead is not a function
    at ProxyServer.<anonymous> (/Users/peteroravec/.nvm/versions/node/v16.17.0/lib/node_modules/netlify-cli/src/utils/proxy.js:310:9)
    at ProxyServer.emit (/Users/peteroravec/.nvm/versions/node/v16.17.0/lib/node_modules/netlify-cli/node_modules/eventemitter3/index.js:204:33)
    at Socket.onOutgoingError (/Users/peteroravec/.nvm/versions/node/v16.17.0/lib/node_modules/netlify-cli/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:157:16)
    at Socket.emit (node:events:525:35)
    at Socket.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

What version of CLI are you using? What site is this? Such issues are more useful if reported on:

Hi,
I updated to latest Netlify CLI… it helped little bit but this issue still there, website is https://eutxo2.netlify.app/

    "netlify": "12.0.1",
    "netlify-cli": "11.3.0",

Could you explain what you mean by “helped little bit”? It was my understanding that either netlify dev crashed or it didn’t, so what behavior are you seeing now, in some detail?

But before you answer that - could you re-update to the newest CLI? We’ve released version 11.5.0 by now :slight_smile:

Hey, I updated to

    "netlify": "12.0.1",
    "netlify-cli": "11.5.0",

but still getting these errors… basically it works for couple of minutes, then crash and keep crashing… most of the time now it not working…

/Users/peteroravec/.nvm/versions/node/v14.18.0/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^

TypeError: res.writeHead is not a function
    at ProxyServer.<anonymous> (/Users/peteroravec/.nvm/versions/node/v14.18.0/lib/node_modules/netlify-cli/src/utils/proxy.js:311:9)
    at ProxyServer.emit (/Users/peteroravec/.nvm/versions/node/v14.18.0/lib/node_modules/netlify-cli/node_modules/eventemitter3/index.js:204:33)
    at Socket.onOutgoingError (/Users/peteroravec/.nvm/versions/node/v14.18.0/lib/node_modules/netlify-cli/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:157:16)
    at Socket.emit (events.js:412:35)
    at Socket.emit (domain.js:475:12)
    at emitErrorNT (net.js:1358:8)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)

Any suggestions?

Hi @peteroravec,

Are you able to share a repro case?

Hard to simulate this… it’s just randomly crash… but I did this…
I reinstalled my macbook and put there fresh instalation…

Installed
“netlify”: “12.0.1”,
“netlify-cli”: “11.5.0”,
“node”: “16.17.0”

Then tried to run it and BUM, same error… on first run on freshly installed computer…

Then I tried older version, netlify-cli@10.17.4 and it works better, I get also that error but maybe once per 3 hours of work…

So, I understand you can sometimes reproduce it, but we cannot, since we don’t have access to your source code :slight_smile:

Let’s try this instead:

  1. come up with the best reproduction case you have - you have versions up there, but not code that shows the problem or steps to trigger it once running, exactly.
  2. once you have that reproduction case - which you might make by whittling down your source code to the smallest thing that causes it, or by opening your repo so people can clone it and test it out for themeslves
  3. …you get a bug filed on our CLI: Issues · netlify/cli · GitHub

That’s what our team would have done for you, but we’ll let you take the lead since only you can access your source code today :slight_smile:

I’m also experiencing this after attempting to update from major version 9 of netlify-cli to 12.5.0. netlify dev starts up fine, but when I load a page that uses a function, the local server exits with

TypeError: res.writeHead is not a function
    at ProxyServer.<anonymous> (file:///home/d4/.nvm/versions/node/v16.14.2/lib/node_modules/netlify-cli/src/utils/proxy.mjs:341:9)
    at ProxyServer.emit (/home/d4/.nvm/versions/node/v16.14.2/lib/node_modules/netlify-cli/node_modules/eventemitter3/index.js:204:33)
    at Socket.onOutgoingError (/home/d4/.nvm/versions/node/v16.14.2/lib/node_modules/netlify-cli/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:157:16)
    at Socket.emit (node:events:538:35)
    at Socket.emit (node:domain:475:12)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
 ELIFECYCLE  Command failed with exit code 7.

In investigating this, I believe Netlify’s proxy implementation may be expecting res for the proxy error handler when it could be a socket, at this point: cli/proxy.mjs at main · netlify/cli · GitHub

An issue describes a similar issue here: confused with the arguments in proxy error-event handler · Issue #1311 · http-party/node-http-proxy · GitHub

I am unfortunately not finding documentation on what parameters could be expected for the proxy.on('error') to confirm, and I need to move on.

If the defensive approach were amenable, Netlify’s error handler could check to see if the writeHead method existed before proceeding. This would at least prevent the local dev server from crashing.

Hi @bdefore welcome to the forums! :wave:t6:

I escalated this to our support engineers in the help desk. Can you please stay tuned to your email for a resolution.

Oh actually, it’s better if you report it directly on the CLI repo, @bdefore. The devs can directly take a look then.

Just ran into this as well - started getting TypeError: res.writeHead is not a function every time I try to run netlify dev. Using netlify-cli v13.0.0. Any updates?

Interesting to hear! That seems like the latest version of the CLI. I wonder if it happens to “all” sites you work on or just a specific one? I am guessing it is something to do with your code/config but trying to figure out more details, so if you saw it working before, or can get it to work on other sites in your team, could you let us know details about that?

Additionally, if you can easily reproduce it, filing a bug report directly on the CLI repo would be a great help to us, since that’s where we’ll end up too :slight_smile: Issues · netlify/cli · GitHub

I’m still having these crashes, so I put together an issue on the CLI repo: Netlify Dev intermittently crashes due to proxy. TypeError: res.writeHead is not a function · Issue #6188 · netlify/cli · GitHub