Skip to content
Related Articles
Open in App
Not now

Related Articles

Node.js util.deprecate() Method

Improve Article
Save Article
  • Last Updated : 01 Dec, 2022
Improve Article
Save Article

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 accept 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 deprecation warning. Some of the deprecated APIs are DEP0001, DEP0001, …., 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: Filename: index.js 

javascript




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


Run index.js file using the following command:

node index.js

Output:

[Deprecation API] DeprecationWarning: someWarningMessage

Example 2: Filename: index.js 

javascript




// Node.js program to demonstrate the
// util.deprecate() method
 
// Import the util module
var util = require('util');
var outerValue = "along with"
 
var 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
);
 
var 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 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 –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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!