Description
Describe the bug
I have for a year now intermittently gottten crashes from Netlify Dev. My initial comment on this is here: https://answers.netlify.com/t/netlify-dev-randomly-getting-typeerror-res-writehead-is-not-a-function-at-proxyserver/73639/12
As I mention there, I believe Netlify Dev's proxy implementation calls writeHead
on the response when that function may not exist, such as if the response is via web socket. As a result, the dev server crashes, sometimes leaving other processes running. In my case, those processes are related to the create-react-app webpack server, for which I have to manually kill in order to properly restart Netlify Dev.
I notice that there was a couple days ago a substantial TypeScript migration, including to the proxy source file. In that update there is a note with a FIXME that looks related: https://github.com/netlify/cli/blob/main/src/utils/proxy.mts#L457 . I'll copy the related source here:
proxy.on('error', (_, req, res) => {
// @ts-expect-error TS(2339) FIXME: Property 'writeHead' does not exist on type 'Socke... Remove this comment to see the full error message
res.writeHead(500, {
'Content-Type': 'text/plain',
})
const message = isEdgeFunctionsRequest(req)
? 'There was an error with an Edge Function. Please check the terminal for more details.'
: 'Could not proxy request.'
res.end(message)
})
A potential solution would be to skip adding the content-type header if writeHead
does not exist on res
.
Some related issues:
#5676
#5917
#6025
Here is the output I encounter from the dev server as it crashes:
› Error: Netlify CLI has terminated unexpectedly
This is a problem with the Netlify CLI, not with your application.
If you recently updated the CLI, consider reverting to an older version by running:
npm install -g netlify-cli@VERSION
You can use any version from https://ntl.fyi/cli-versions.
Please report this problem at https://ntl.fyi/cli-error including the error details below.
TypeError: res.writeHead is not a function
at ProxyServer.<anonymous> (file:///home/d4/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/src/utils/proxy.mjs:380:13)
at ProxyServer.emit (/home/d4/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/node_modules/eventemitter3/index.js:204:33)
at Socket.onOutgoingError (/home/d4/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:157:16)
at Socket.emit (node:events:526:35)
at Socket.emit (node:domain:488:12)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at processTicksAndRejections (node:internal/process/task_queues:82:21)
System:
OS: Linux 6.5 Manjaro Linux
CPU: (16) x64 AMD Ryzen 7 5800X3D 8-Core Processor
Binaries:
Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn
npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
Browsers:
Firefox: 119.0.1
npmGlobalPackages:
netlify-cli: 17.4.0
ELIFECYCLE Command failed with exit code 1.
Steps to reproduce
Unfortunately, I cannot share my source and cannot consistently reproduce the crash. I have not been able to reproduce in a minimal project from scratch. It appears to occur more frequently when I am calling particular Netlify Functions, but that may be unrelated.
Configuration
Partial netlify.toml:
[functions]
node_bundler = "esbuild"
[dev]
command = "pnpm start:dev" # Command to start your dev server
targetPort = 8080 # The port for your application server, framework or site generator
publish = "build/" # The path to your static content folder
autoLaunch = false # a Boolean value that determines if Netlify Dev launches the local server address in your browser
framework = "#custom" # https://cli.netlify.com/netlify-dev/#project-detection
# to enable https local, see README for instructions
[dev.https]
certFile = ".cert/server.crt"
keyFile = ".cert/server.key"
The pnpm start:dev
command is calling react-scripts start
Environment
System:
OS: Linux 6.5 Manjaro Linux
CPU: (16) x64 AMD Ryzen 7 5800X3D 8-Core Processor
Memory: 28.31 GB / 31.25 GB
Container: Yes
Shell: 5.2.15 - /bin/bash
Binaries:
Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn
npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
pnpm: 8.10.5 - ~/.nvm/versions/node/v20.9.0/bin/pnpm
npmGlobalPackages:
netlify-cli: 17.4.0