Skip to main content

deprecate

function deprecate
Jump to headingdeprecate<T extends Function>(
fn: T,
msg: string,
code?: string,
): T

The util.deprecate() method wraps fn (which may be a function or class) in such a way that it is marked as deprecated.

import util from 'node:util';

exports.obsoleteFunction = util.deprecate(() => {
  // Do something here.
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');

When called, util.deprecate() will return a function that will emit a DeprecationWarning using the 'warning' event. The warning will be emitted and printed to stderr the first time the returned function is called. After the warning is emitted, the wrapped function is called without emitting a warning.

If the same optional code is supplied in multiple calls to util.deprecate(), the warning will be emitted only once for that code.

import util from 'node:util';

const fn1 = util.deprecate(someFunction, someMessage, 'DEP0001');
const fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
fn1(); // Emits a deprecation warning with code DEP0001
fn2(); // Does not emit a deprecation warning because it has the same code

If either the --no-deprecation or --no-warnings command-line flags are used, or if the process.noDeprecation property is set to trueprior to the first deprecation warning, the util.deprecate() method does nothing.

If the --trace-deprecation or --trace-warnings command-line flags are set, or the process.traceDeprecation property is set to true, a warning and a stack trace are printed to stderr the first time the deprecated function is called.

If the --throw-deprecation command-line flag is set, or the process.throwDeprecation property is set to true, then an exception will be thrown when the deprecated function is called.

The --throw-deprecation command-line flag and process.throwDeprecation property take precedence over --trace-deprecation and process.traceDeprecation.

Type Parameters Jump to heading

Jump to headingT extends Function

Parameters Jump to heading

The function that is being deprecated.

Jump to headingmsg: string

A warning message to display when the deprecated function is invoked.

optional
Jump to headingcode: string

A deprecation code. See the list of deprecated APIs for a list of codes.

Return Type Jump to heading

The deprecated function wrapped to emit a warning.

Back to top