Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Method Deprecated Warning in Nodejs

| Comments

Recently was tweaking a node module and had to warn users on deprecated methods, truns out it's damn easy with util module.

Pinch of code :

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env node
#__filename = hello.js

util=require('util');

exports.yo = function(){
    console.log("yo yo!");
};

exports.hello = util.deprecate(function() {
  return exports.yo();
}, '`hello` is depreciated, please use `yo` instead.');

Let's import hello.js and try invoking hello :

1
2
3
4
5
> greet = require("hello");
{ yo: [Function], hello: [Function: deprecated] }
> greet.hello()
`hello` is depreciated, please use `yo` instead.
yo yo!

Happy hacking!

Comments