DEP0066: code that reads res._headers breaks on current Node.js
[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecatedShort answer
DEP0066 covers the undocumented properties _headers and _headerNames on OutgoingMessage, such as res._headers. Node.js lists them as End-of-Life, so code that reads them must switch to the public methods: getHeaders(), getHeader(), getHeaderNames(), hasHeader(), setHeader() and removeHeader().
Fix it fast
1Find out which cause you have
Run the app with --throw-deprecation. Node then throws at the first use, and the stack trace shows whether your code or a dependency reads the property.
node --throw-deprecation server.js2Match what you see, then jump to its fix
- 1
The stack trace points to a file in your projectYour own code reads res._headers or res._headerNamesFix - 2
The stack trace points into node_modulesAn old dependency reads the propertyFix - 3
The warning is hidden with --no-deprecation but the code still uses the propertySilencing the warning instead of fixing the codeFix
- 1
What does the error look like?
[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
Cause 1 of 3
Your own code reads res._headers or res._headerNames
1Check
The Node.js deprecation list says these properties were never documented as supported, and names the public methods to use instead.
grep -rn "_headers\|_headerNames" src/ lib/ app.js server.js 2>/dev/null2Fix
Replace each use with the public method that fits.
// before
const type = res._headers['content-type'];
// after
const type = res.getHeader('content-type');
const all = res.getHeaders();
const names = res.getHeaderNames();3Verify
Start the app with --throw-deprecation and make a request: no error.
node --throw-deprecation server.jsConfirmed by raw.githubusercontent.com
Cause 2 of 3
An old dependency reads the property
1Check
Find which package owns the file in the trace and which version is installed.
npm ls --all | grep -i "<package-name>"
npm outdated2Fix
Update the package to a release that no longer uses the property, or replace it. The deprecation list marks the property End-of-Life, so waiting is not a plan.
npm install <package-name>@latest3Verify
Run again with --throw-deprecation and exercise the route that failed.
node --throw-deprecation server.jsConfirmed by raw.githubusercontent.com
Cause 3 of 3
Silencing the warning instead of fixing the code
1Check
--no-deprecation only silences deprecation warnings. It does not restore removed behaviour.
grep -rn "no-deprecation" package.json Dockerfile ecosystem.config.js 2>/dev/null2Fix
Remove the flag once the code uses public methods, so future deprecations stay visible.
# package.json
"start": "node server.js"3Verify
A clean start shows no DEP warning.
node server.jsConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Run tests with --throw-deprecation so a deprecated call fails CI.
- Only use the documented methods of http.ServerResponse.
- Keep dependencies current and read the Node.js deprecation list when you raise the Node.js major version.
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