Is there a way to match a compressed javascript stack trace with a source map to pinpoint the correct error message?

When managing our production server, I have implemented minified javascript without including a map file to prevent users from easily deciphering errors.

To handle angular exceptions caught by $exceptionHandler, I developed a logging service that forwards the stack traces to my email. Unfortunately, the stack trace provided is difficult to read:

n is not defined
    at o (http://localhost:9000/build/app.min.js:1:3284)
    at new NameController (http://localhost:9000/build/app.min.js:1:3412)
    at e (http://localhost:9000/build/bower.min.js:44:193)
    at Object.g.instantiate (http://localhost:9000/build/bower.min.js:44:310)
    at b.$get (http://localhost:9000/build/bower.min.js:85:313)
    at d.compile (http://localhost:9000/build/bower.min.js:321:23333)
    at aa (http://localhost:9000/build/bower.min.js:78:90)
    at K (http://localhost:9000/build/bower.min.js:67:39)
    at g (http://localhost:9000/build/bower.min.js:59:410)
    at http://localhost:9000/build/bower.min.js:58:480 <ui-view class="ng-scope">

I am curious if there is a tool available that can match this stack trace with the original non-minified source code using a map file or another method.

Answer №1

Realizing there was a lack of simple tools available for converting a minified stack trace to a readable one using a source map without relying on a web service, I decided to create my own solution:

https://github.com/mifi/stacktracify

To get started, simply install the tool by running the following command:

npm install -g stacktracify

Once installed, copy your minified stack trace to your clipboard and execute the following command:

stacktracify /path/to/js.map

Answer №2

To uncover the hidden gems of source code, one must dive into the world of source maps. Forget about web browsers - all you need is to decode the minified references into their full-bodied counterparts.

If you're familiar with NodeJS, there's a handy package waiting for you to make this task a breeze.

https://github.com/mozilla/source-map/

First, install the library by running:

npm install -g source-map

or

yarn global add source-map

Next, craft a file named "issue.js"

fs = require('fs');
var sourceMap = require('source-map');
var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8"));
console.log(smc.originalPositionFor({line: 1, column: 3284}));

Execute the script using node:

node issue.js

Voilà! The console will unveil the original file location corresponding to the first line from the stack trace.

Just a tip: I recommend installing source-map globally for convenience, but feel free to incorporate it directly into your Node project for a more localized approach.

Answer №3

Expanding on the solution provided by @Reactgular, the following code snippet is compatible with the most recent version of source-map.

  const rawSourceMap = fs.readFileSync("./app.min.js.map","utf8");

  const whatever = sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
    console.log(consumer.originalPositionFor({
      line: 1,
      column: 3284
    }));
  });

In addition to the conversation in the thread, a basic regex like /\/(\w*[-\.]?\w*).js:\d*:\d*/g can be used.

Here is a straightforward regex to identify all line numbers in a stack trace.

  //regex for patterns like utils.js, utils123.js, utils-name.js, utils.version.js
  var patt = /\/(\w*[-\.]?\w*).js:\d*:\d*/g; 
  // returns matches like ['/app.min.js:1:3284', '/bower.min.js:44:193', ..]
  var errorPositions = line.match(patt);
  console.log(errorPositions);
  if(!errorPositions || errorPositions.length === 0) {
      console.log("No error line numbers detected in the file. Ensure your stack trace file is proper");
      return;
  }
  errorPositions.forEach(function(error) {
    findInSourceMap(error);
  });
});

Answer №4

If you were able to access the source map file from an external source and maintain the same file structure, you might be able to figure it out with some effort. However, as far as I know, there aren't any tools outside of the browser that can assist with this.

One added benefit of having the data within a live browser environment is the ability to check locals, which isn't possible even with a source map.

Consider using a tool like rollbar for error reporting. This tool will provide detailed reports of all locals in each frame to aid in debugging. It also offers support for utilizing sourcemaps outside of the browser to address security concerns.

Answer №5

If you're struggling with reading stack traces, I've developed a handy web tool just for that - check it out at .

All you have to do is paste your stack trace and upload your source maps, then the tool will display the "original" stack trace for you.

And best of all, this tool runs directly in your browser and is completely free to use.

Answer №6

Include a comment directive in your JavaScript code for referencing the source map file.

//# sourceMappingURL=/path/to/your/sourcemap.map

In Firefox (not certain about Chrome), you can enable source maps by clicking on the "Debugger settings" button and choosing "Show original sources" from the settings menu that appears:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular Handsontable experiencing issues when used with standard scope variables

In my Angular application, I have a scope variable named "xd" that I want to display in a table. When using a traditional table structure, everything works perfectly: <table class="table"> <tr ng-repeat="x in xd"> <th ng-bind="x.title ...

Tips for generating a node for the activator attribute within Vuetify?

Vuetify offers the 'activator' prop in multiple components like 'v-menu' and 'v-dialog', but there is limited information on how to create a node for it to function correctly. The documentation states: Designate a custom act ...

"Create a React button component that, when clicked, navig

I'm currently developing a web application using React and bootstrap. I'm facing difficulties in redirecting the page to another one when applying onClick to buttons. After adding a href, I'm unable to navigate to another page. Do you think ...

Pass an item from one controller to another using a service

Hey there, I'm new to AngularJs and trying to figure out how to send an object from one controller to another using a service. However, I'm running into an issue where the value of the object (SessionUser) isn't updating. Can anyone help me ...

Assigning a specific data type value to an element as it enters the top of the viewport

I have a unique color code stored for each section, and when a section reaches the top of the screen (specifically -180px for the header), I want to dynamically change the text color of the header element as you scroll through the sections. Despite no erro ...

Steps on closing an Angular strap modal after submitting a form:

<div class="modal" tabindex="-1" role="dialog"> <div class="modal-dialog "> <div class="modal-content"> <div ng-controller="externalController"> <form role="form" name="detailForm" ...

Discover the steps to incorporate an external JS file into Next.js 12

I am encountering an issue in my Next.js project when trying to import a local JS file. Here is the code snippet I am using: <Script type="text/javascript" src="/js.js"></Script> Unfortunately, this approach results in the ...

Harvest Data from JSON Data Structure

My query pertains to handling data received from a URL in JSON format. Most examples I've come across focus on recursively printing symmetrical JSON objects, like this one. However, how can I effectively print the contents of the following JSON object ...

Simulating server-side interactions in Node.js with TestCafe

I am currently working on a project where I need to figure out how to mock server-side requests. While I have successfully managed to mock client-side requests using request hooks, I am facing challenges when it comes to intercepting server-side requests ...

limitation in bootstrap ensures that only one collapse element is open at any

Hey, can someone help me fix this code? I'm trying to make it so that only one element opens at a time, but I can't seem to figure out how. Right now, if I click on two elements, they both open and the other one doesn't close. This is what ...

Retrieving data from a file results in receiving blank strings

Struggling to access the data within a directory of files, I've encountered an issue where the data doesn't seem to be read correctly or at all. Even though there is content when opening the files individually, when attempting to examine their co ...

Remove item from jQuery's "raw text" HTML

Suppose I have the following JavaScript string: var string = '<div id="div1"><div id="div2"></div></div>'; Is it possible to utilize jQuery in order to create a new string as shown below? var newString = '<div i ...

history.push() function is ineffective within a JavaScript file that does not contain a class

I've been delving into React and encountering an issue with the history.push("/dashboard") method, it's not functioning as expected. import axios from "axios"; import { GET_ERRORS, GET_PROJECT, GET_PROJECTS } from "./types"; export const createP ...

Visualizing data with a grouped bar chart in D3.js

I am currently working on creating a vertical bar chart using D3.js, similar to the one shown in this (source: statcan.gc.ca) However, I am facing an issue as I am unable to display two sets of data for comparison. Following a tutorial from , I have cr ...

Adjust positioning of navigation when hovered over

Need help creating a cool navigation effect like this. Live example: https://hookandbarrelrestaurant.com/ Here is my code: https://codepen.io/Dhaval182/pen/rQPMoW ...

Selecting dates based on week number

Is there a method in asp.net or via javascript to capture the dates (Monday to Friday) when a user clicks on the week number displayed on the calendar and display them on a label? ...

Combining multiple JSON objects in an array into one single object with the help of jQuery

My array consists of JSON objects like the ones shown below: [{ "Name": "Nikhil", "Surname": "Agrawal" }, { "profession": "java developer", "experience": "2 years" }, { "company": "xyz", "city": "hyderabad" }] What I aim to achiev ...

What could be causing ngInfiniteScroll to not work properly with tables?

I've been attempting to incorporate ngInfiniteScroll into my table using ng-repeat on <tbody> however, it isn't triggering when I reach the end of the page. <div infinite-scroll="list.getMoreItems()"> <table md-table md-row-se ...

Using Polymer in Chrome Extension content script

Currently, I am experimenting with incorporating Polymer into a chrome extension. My main objective is to take advantage of styling encapsulation in order for my content scripts to operate independently from the CSS on the visited webpage. To begin, I hav ...

Explore within another map and analyze the node to spot the differences

I have some questions about iterating through a JavaScript Object and using array functions in JavaScript. Let's assume I have the following variables: var json1 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]"; var json2 = "[{"id": 1, "name":"x"}, ...