console.trace()
outputs its result on console.
I want to get the results as string and save them to a file.
I don't define names for functions and I also can not get their names with callee.caller.name
.

- 4,671
- 6
- 44
- 61
-
1this doesn't work in PhantomJS :( – ekkis Feb 17 '16 at 20:18
7 Answers
I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace
. (More info here)
So a hacky way to get it would be:
var getStackTrace = function() {
var obj = {};
Error.captureStackTrace(obj, getStackTrace);
return obj.stack;
};
console.log(getStackTrace());
Normally, getStackTrace
would be on the stack when it's captured. The second argument there excludes getStackTrace
from being included in the stack trace.

- 14,322
- 3
- 32
- 24
-
20Thank you for your information. That worked in chrome but didnt in firefox. So i searched again and found `Error().stack`. Though Object and function names are lost in firefox and object name is lost in chrome(same as `Error.captureStackTrace`), `Error().stack` works both browsers and it gives me enough information to debug. – js_ Jul 17 '11 at 03:20
-
Exact the same result as answer of @Konstantin Smolyanin. Same limited details as a result. – Codebeat Sep 04 '18 at 01:23
-
1This shouldn't be the accepted answer. The stack you get here is "cut down" containing only a "top portion", while console.trace() will show the full stack. See an example with stack depth 30 here: https://stackoverflow.com/questions/62768598/condensed-version-of-console-trace – mathheadinclouds Jul 07 '20 at 06:10
-
Error.stack is what you need. It works in Chrome and Firefox. For example
try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
will give in Chrome:
TypeError: Object #<Object> has no method 'debug'
at eval at <anonymous> (unknown source)
at eval (native)
at Object._evaluateOn (unknown source)
at Object._evaluateAndWrap (unknown source)
at Object.evaluate (unknown source)
and in Firefox:
@http://www.google.com.ua/:87 _firebugInjectedEvaluate("with(_FirebugCommandLine){try { var a = {}; a.debug() } catch(ex) {console.log(ex.stack)}\n};")
@http://www.google.com.ua/:87 _firebugEvalEvent([object Event])
@http://www.google.com.ua/:67

- 4,930
- 1
- 46
- 76

- 22,277
- 3
- 72
- 89
-
3Thanks for your answer. But that works only when exception occurred. I need to get stack trace without exception. – js_ Jul 17 '11 at 03:23
-
14
-
1This should throw an exception on a.debug() -- it's an expensive way to get the stack, but should work. – fijiaaron Jul 12 '12 at 20:02
-
This approach also comes in handy when trying to get a trace from some code that can only run on, e.g. PhantomJS or the like for whatever reason. – waxspin Dec 31 '14 at 19:11
This will give a stack trace (as array of strings) for modern Chrome, Firefox, Opera and IE10+
function getStackTrace () {
var stack;
try {
throw new Error('');
}
catch (error) {
stack = error.stack || '';
}
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
Usage:
console.log(getStackTrace().join('\n'));
It excludes from the stack its own call as well as title "Error" that is used by Chrome and Firefox (but not IE).
It shouldn't crash on older browsers but just return empty array. If you need more universal solution look at stacktrace.js. Its list of supported browsers is really impressive but to my mind it is very big for that small task it is intended for: 37Kb of minified text including all dependencies.

- 17,579
- 12
- 56
- 56
you only need var stack = new Error().stack
. this is simplified version of @sgouros answer.
function foo() {
bar();
}
function bar() {
baz();
}
function baz() {
console.log(new Error().stack);
}
foo();
Probably will not work in every browser (works in Chrome).

- 61,973
- 54
- 229
- 402
There is a library called stacktrace.js that gives you cross browser stack traces. You can use it simply by including the script and calling at any point:
var trace = printStackTrace();

- 4,311
- 5
- 27
- 50

- 5,015
- 3
- 35
- 28
-
1I would look at https://github.com/stacktracejs/stacktrace.js as the implementation has changed to support ES6 promises. – Erez Cohen Aug 23 '15 at 14:17
-
Note that for now this should be used: https://github.com/stacktracejs/stacktrace.js/tree/stable?files=1 (new version hasn't been released yet) – Erez Cohen Aug 23 '15 at 15:24
This is only a minor enhancement to Konstantin's excellent code. It cuts a bit on the expense of throwing-catching and just instantiates the Error stack:
function getStackTrace () {
let stack = new Error().stack || '';
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
I usually want a specific level of stack trace (for my custom logger) so this is also possible when calling:
getStackTrace()[2]; // get stack trace info 2 levels-deep

- 494
- 4
- 9
I was trying to get Stack Trace as string variable in JavaScript on NodeJS and this tutorial helped me. This will work in your scenario as well except stack trace is printed via Error Object than console.trace()
.
Code to print stack trace:
function add(x, y) {
console.log(new Error().stack);
return x+y;
}

- 2,378
- 5
- 28
- 37
-
1This is more accurate and cross-platform, it works even at nodejs. – Andres Felipe Oct 25 '21 at 19:38