DeprecationWarning: Buffer() is deprecated in Node.js
DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.Short answer
Node.js warns when code calls Buffer() or new Buffer(). Replace it with Buffer.from for existing data, Buffer.alloc for zero-filled memory, or Buffer.allocUnsafe when you fill the memory yourself. If the call is in a dependency, run with --trace-deprecation to find which package, then update it.
Fix it fast
1Find out which cause you have
Print the stack trace of the warning to see which file made the call.
node --trace-deprecation app.js2Match what you see, then jump to its fix
- 1
The trace points at a file in your project, not node_modules.Your own code calls Buffer() or new Buffer()Fix - 2
The trace path contains node_modules/<package>.A dependency calls Buffer() and pending-deprecation mode is onFix - 3
The same code was silent on the old server and warns on the new one.The script moved to a Node.js version that prints runtime warningsFix
- 1
What does the error look like?
DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.[DEP0005] DeprecationWarning
Cause 1 of 3
Your own code calls Buffer() or new Buffer()
1Check
Search the source for both forms.
grep -rn "new Buffer(\|Buffer(" src --include=*.js2Fix
Use the replacement that matches the argument: a string or array uses Buffer.from, a size uses Buffer.alloc. The docs describe Buffer.allocUnsafe as faster but with uninitialized memory, so fill it before you read it.
// before
const a = new Buffer(10);
const b = new Buffer('hello', 'utf8');
// after
const a2 = Buffer.alloc(10);
const b2 = Buffer.from('hello', 'utf8');3Verify
Run the app again with the trace flag; no DEP0005 line should appear.
node --trace-deprecation app.jsConfirmed by nodejs.org
Cause 2 of 3
A dependency calls Buffer() and pending-deprecation mode is on
1Check
Without the --pending-deprecation flag, Node.js prints this warning only for code outside node_modules. A warning that names a dependency means the flag or NODE_PENDING_DEPRECATION=1 is set.
echo "$NODE_PENDING_DEPRECATION"
grep -n "pending-deprecation" package.json2Fix
Update the named package to a release that no longer uses Buffer(). If you do not need the early-warning mode, remove the flag or variable.
npm install <package-name>@latest3Verify
Run with the trace flag again.
node --trace-deprecation app.jsConfirmed by nodejs.org
Cause 3 of 3
The script moved to a Node.js version that prints runtime warnings
1Check
The deprecation was documentation-only from Node.js v6.0.0 and became a runtime warning in v10.0.0, so older servers never printed it.
node --version2Fix
Fix the code as in the first cause. Do not hide it with --no-deprecation as a fix; that only silences all deprecation warnings.
# replace Buffer() calls, then redeploy3Verify
No warning on the new server.
node --trace-deprecation app.jsConfirmed by nodejs.org
How do I stop it coming back?
- Never use new Buffer in new code; use Buffer.from, Buffer.alloc or Buffer.allocUnsafe.
- Run CI once with --pending-deprecation to catch problems early.
- Keep dependencies updated so deprecated calls disappear with them.
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