util.deprecate()

Wraps a function so that calling it emits a one-time deprecation warning.

Since Node 0.8 Spec ↗

Syntax

util.deprecate(fn, message[, code])

Parameters

NameTypeRequiredDescription
fn function Yes The function to mark as deprecated.
message string Yes The warning text shown the first time it is called.
code string No A deprecation code so users can silence or trap it.

Returns

function — A wrapped function that warns then delegates.

Examples

import { deprecate } from 'node:util';

const oldApi = deprecate(
  (x) => x * 2,
  'oldApi() is deprecated, use double()',
  'MYLIB001',
);
console.log(oldApi(21));
Output
(node:1) [MYLIB001] DeprecationWarning: oldApi() is deprecated, use double()
42

Notes

The warning is emitted only once per process. Running Node with `--throw-deprecation` turns it into an error (handy in tests) and `--no-deprecation` silences it. Intended for library authors.

See also