A Node.js server answers 431 Request Header Fields Too Large
Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)Short answer
431 means the server refused the request because its headers were too large. Node.js answers 431 when the request headers go past its limit, 16 KiB by default. Measure the headers, remove what is oversized (usually cookies or a long token), and only then raise the limit with --max-http-header-size.
Fix it fast
1Find out which cause you have
Repeat the failing request with curl and print the size of what you send. Compare it with the 16 KiB limit.
curl -sI -H "Cookie: <paste the Cookie header value from the browser>" http://localhost:3000/ | head -n 12Match what you see, then jump to its fix
What does the error look like?
Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)HTTP/1.1 431 Request Header Fields Too Large
Cause 1 of 2
One header, usually cookies or a token, is oversized
1Check
The standard says the client may send the request again after making its header fields smaller. Open the browser's network tab and look at the Cookie and Authorization headers on the failing request.
printf '%s' "<paste the Cookie header value>" | wc -c2Fix
Stop storing large data in cookies and tokens, delete the stale cookies for the host and sign in again.
// store a short id in the cookie, keep the data on the server
res.cookie('sid', sessionId, { httpOnly: true });3Verify
Repeat the request; the status is 200 instead of 431.
curl -I http://localhost:3000/Confirmed by rfc-editor.org
Cause 2 of 2
The headers are legitimate but over Node.js's size limit
1Check
Node.js rejects requests whose headers exceed its limit, which is 16 KiB by default and can be changed with --max-http-header-size. Check how the process is started in your scripts or process manager.
grep -rn max-http-header-size package.json2Fix
Start the server with a larger limit in bytes. Treat this as a stopgap and still reduce the header size.
node --max-http-header-size=32768 server.js3Verify
The same request now gets a normal response.
curl -I http://localhost:3000/Confirmed by nodejs.org
How do I stop it coming back?
- Keep cookies and bearer tokens small; keep session data on the server.
- Clear cookies for localhost between projects, since the browser sends every cookie for the host to every app on it.
- Do not raise the header limit globally without a reason; large limits make abuse easier.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix