Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

DeprecationWarning: Buffer() is deprecated in Node.js

ErrorDeprecationWarning: 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.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the stack trace of the warning to see which file made the call.

    node --trace-deprecation app.js
  2. 2Match what you see, then jump to its fix

    1. 1The trace points at a file in your project, not node_modules.Your own code calls Buffer() or new Buffer()Fix
    2. 2The trace path contains node_modules/<package>.A dependency calls Buffer() and pending-deprecation mode is onFix
    3. 3The 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

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=*.js

2Fix

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.js

Confirmed by nodejs.org

Did this fix it?

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.json

2Fix

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>@latest

3Verify

Run with the trace flag again.

node --trace-deprecation app.js

Confirmed by nodejs.org

Did this fix it?

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 --version

2Fix

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 redeploy

3Verify

No warning on the new server.

node --trace-deprecation app.js

Confirmed by nodejs.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.