JavaScript 查看stack trace

How can I get a JavaScript stack trace when I throw an exception?

Edit 2 (2017):

In all modern browsers you can simply call: console.trace(); (MDN Reference)

stacktrace.js

Generate, parse, and enhance JavaScript stack traces in all web browsers https://www.stacktracejs.com/

Debug and profile your JavaScript with a stack trace of function calls leading to an error (or any condition you specify).

stacktrace.js uses browsers' Error.stack mechanism to generate stack traces, parses them, enhances them with source maps and uses Promises to return an Array of StackFrames.

bower init

bower install stacktrace-js --save

或者

https://cdnjs.com/libraries/stacktrace.js  但是这是一个html页面

Resource blocked due to MIME type mismatch (X-Content-Type-Options: nosniff)

Check if the file path is correct and the file exists - in my case that was the issue - as I fixed it, the error disappeared

访问html页面,拿到https://cdnjs.cloudflare.com/ajax/libs/stacktrace.js/2.0.0/stacktrace.js

获取当前位置的堆栈信息

Get a stack trace from current location

var callback = function(stackframes) {
    var stringifiedStack = stackframes.map(function(sf) {
        return sf.toString();
    }).join('
');
    console.log(stringifiedStack);
};

var errback = function(err) { console.log(err.message); };

StackTrace.get().then(callback).catch(errback);
//===> Promise(Array[StackFrame], Error)
//===> callback([
//    StackFrame({functionName: 'func1', args: [], fileName: 'file.js', lineNumber: 203, columnNumber: 9}), 
//    StackFrame({functionName: 'func2', args: [], fileName: 'http://localhost:3000/file.min.js', lineNumber: 1, columnNumber: 3284})
//])
原文地址:https://www.cnblogs.com/chucklu/p/11088630.html