Looking for a node.js package that handles stack tracing similar to how this is done in RoR:
-
What "stack trace package" in Rails are you talking about? – Andrew Marshall Mar 04 '12 at 01:57
-
I updated the question to add a link. – Chris Abrams Mar 04 '12 at 02:03
-
Possible duplicate of [How to print a stack trace in Node.js?](http://stackoverflow.com/questions/2923858/how-to-print-a-stack-trace-in-node-js) – Gustavo Straube Apr 29 '16 at 13:15
4 Answers
You can get this text off of the .stack
property from any Error. For instance:
try {
throw new Error();
} catch (e) {
console.log(e.stack);
}
or just new
up an error for the purposes of getting the stack trace
console.log(new Error().stack)

- 8,705
- 5
- 46
- 82
-
It would need to be `(new Error()).stack`. `new Error().stack` would get `Error().stack` and then try to construct something out of it – McKayla Jul 10 '12 at 04:20
-
6
-
1You should actually use `console.error` instead of `console.log`. They may appear to do the same thing, but actually using the former outputs to `stderr` instead of `stdout`. – 0x8890 Oct 26 '14 at 06:43
-
Especially useful in `electron` applications where `console.trace()` doesn't always work. – x-yuri Jul 15 '19 at 12:18
If you use winston, you can add this:
winston = require('winston');
logger = expandErrors(new winston.Logger());
logger.info(new Error("my error"));
// Extend a winston by making it expand errors when passed in as the
// second argument (the first argument is the log level).
function expandErrors(logger) {
var oldLogFunc = logger.log;
logger.log = function() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= 2 && args[1] instanceof Error) {
args[1] = args[1].stack;
}
return oldLogFunc.apply(this, args);
};
return logger;
}
and then you get winston loggers with stack traces. Also is in a gist.

- 2,230
- 2
- 20
- 18
there's a function for that: console.trace()
In case you don't want to log to console you can get the stack trace string value using new Error().stack

- 4,099
- 1
- 30
- 31
-
-
Ya, but what if you need to like.. iono.. pass it around, or do something with it? The console functions are called "helper" functions for a reason - they're just so you can be really lazy in specific cases ; ) – B T Apr 11 '13 at 02:43
-
that sounds like a big "what if" based on original question, but let me update for that. – Benja Apr 11 '13 at 02:59
-
Not a big whatif at all. I report my error messages to a centralized logging store. Thank you much for the update! – Michael Lang Aug 30 '13 at 22:10
Check out callsite, this grabs the stack object so you can use it any way you like (and as an object, rather than a string). Its pretty awesome, and nice and simple. Check out this for more info on playing with the stack
There are a few excellent loggers out there for Node.js already, but I built a logger that outputs a colorized, simple output and brief stack trace. It doesn't override console.log, which I find to be handy. If you're interested, you can find it here, node-logger.

- 2,797
- 2
- 21
- 25
-
The implementation of callsite is consistent with the suggestion from @Benja. – Casey Watson Feb 01 '17 at 18:53