strange error: Cowardly refusing to pack object with circular reference

I'm trying to create a prototype method to get nested properties in an object (or return null if not exists).

Object.prototype.getNested = function() {
  var args = Array.prototype.slice.call(arguments);
  var obj = this;
  for (var i = 0; i < args.length; i++) {
    if (!obj.hasOwnProperty(args[i])) return null;
    obj = obj[args[i]];
  }
  return obj;
};

So i'm getting this error:

uncaughtException: Cowardly refusing to pack object with circular reference getNested

What does this error means? I've never seen this before.

OK, my guess is this error message is coming from the msgpack npm module as indicated in the source code here. What it means is you are trying to serialize an object that refers to itself. Here's me reproducing this error in the node REPL:

> var msgpack = require("msgpack");    
> var obj = {};
> obj["me"] = obj;
{ me: [Circular] }
> msgpack.pack(obj);
TypeError: Cowardly refusing to pack object with circular reference
    at Object.pack (/Users/plyons/projects/stackoverflow/node_modules/msgpack/lib/msgpack.js:30:18)
    at repl:1:9
    at REPLServer.self.eval (repl.js:110:21)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)

You need to debug into that stack and see which exact object is getting passed to msgpack.pack then you can understand the real root cause. It might just be a matter of when you add getNested as a property, make sure to mark it non-enumerable.