Open In App

Node.js util.deprecate() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The util.deprecate() (Added in v0.8.0_ method is an inbuilt application programming interface of the util module which wraps fn (which may be a function or class) in such a way that it is marked as deprecated. When util.deprecate() method is called, it returns a function that emits a DeprecationWarning using the ‘warning’ event. The first time the returned function is called, the warning is emitted and printed to stderr. Once the warning is emitted, then the wrapped function is called. The warning is emitted only once if the same optional code is supplied in multiple calls to ‘util.deprecate()‘.

Syntax:

util.deprecate(fn, msg, )

Parameters: This function accepts three parameters as mentioned above and described below:

  • fn: It is the <function> that is being deprecated.
  • msg: When the deprecated function is invoked, a ‘warning message’ <string> is needed to be displayed.
  • code: It is a deprecation code <string> that is printed along with a deprecation warning. Some of the deprecated APIs are DEP0001, DEP0001, …., and DEP0143.

Return Value: It returns the deprecated function wrapped to emit a warning, i.e. the warning message is returned along with deprecated APIs after running the complete function.

Example 1: In this example, we will see the use of the util.deprecate() method

Filename: index.js 

javascript




// Node.js program to demonstrate the
// util.deprecate() method
 
// Import the util module
const util = require('util');
 
const deprecateFunction = util.deprecate(
    // Function which is deprecated
    function () { },
 
    // Warning message that is
    // printed to stderr
    "someWarningMessage",
 
    // Deprecated API
    'Deprecation API'
);
 
// Function call
deprecateFunction();


Run the index.js file using the following command:

node index.js

Output:

[Deprecation API] DeprecationWarning: someWarningMessage

Example 2: In this example, we will see the use of the util.deprecate() method

Filename: index.js 

javascript




// Node.js program to demonstrate the
// util.deprecate() method
 
// Import the util module
const util = require('util');
let outerValue = "along with"
 
const geekyFunction1 = util.deprecate(() => {
    console.log("This Deprecated function is working, ");
},
    // The arrow function which is deprecated
    "geekyFunction() is deprecated. Use "
    + "geeksForGeeksFunction() instead",
 
    // The warning message that is printed
    // to stderr
    'DEP0014' // Deprecated API
);
 
const geekyFunction2 = util.deprecate(function (call) {
    console.log("This Deprecated function is working, ",
        outerValue, call());
},
    // The function which is deprecated
    "geekyFunction() is deprecated. Use "
    + "geeksForGeeksFunction() instead",
    // The warning message that is printed to stderr
    'DEP0014' // Deprecated API
);
 
// Function call
geekyFunction1();
 
// Function call
geekyFunction2(function call() {
    console.log("Callback");
    return "return value"
});


Run the index.js file using the following command:

node index.js

Output:

This Deprecated function is working, Callback This Deprecated function is working, along with return value [DEP0014] DeprecationWarning: geekyFunction() is deprecated. Use geeksforgeeksFunction() instead

Conditions:

  • A stack trace and a warning are printed to stderr the first time if trace-deprecation/trace-warnings cmd flags are set or the process.traceDeprecation property is set to true.
  • An exception will be thrown, if the –throw-deprecation cmd flag is set, or the process.throwDeprecation is set to true.
  • The util.deprecate() does nothing if the process.noDeprecation property is set to true, or no-deprecation/no-warnings cmd flags are used.
  • trace-deprecation and process.traceDeprecation have lower precedence than –throw-deprecation cmd flag and process.throwDeprecation property.

Reference: https://nodejs.org/api/util.html#util_util_deprecate_fn_msg_code



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads