---
title: "How can I find a readable stack trace to debug TypeScript and JavaScript when using a bundler like Vite or Webpack?"
description: "Debugging is an essential part of any development process, and stack traces are often the first tool developers look at when errors occur. However, when using…"
url: https://www.danny.engineering/answer/javascript/how-can-i-find-a-readable-stack-trace-to-debug-typescript-and-javascript-when-using-a-bundler-like-vite-or-webpack
author: Danny
published: 2025-02-04T00:00:00.000Z
updated: 2025-02-04T20:00:00.000Z
tags: ["Javascript"]
---

# How can I find a readable stack trace to debug TypeScript and JavaScript when using a bundler like Vite or Webpack?

Debugging is an essential part of any development process, and stack traces are often the first tool developers look at when errors occur. However, when using modern bundlers like Vite or Webpack, stack traces can be difficult to comprehend due to code minification and transformations. Understanding how to make stack traces more readable can save valuable time and accelerate debugging.

## Problem

When using a bundler like Vite or Webpack, the original code is often transformed into a format that browsers can execute more efficiently. This involves minifying variable names, combining files, and applying other optimisations. As a result, when an error occurs during runtime, the generated stack trace points to locations in the bundled file, which is difficult—if not impossible—to debug directly.

For instance, an error might look like this:

```plaintext
TypeError: Cannot read properties of undefined (reading 'foo')
    at o (app.8761343d.js:1:2182)
    at Object.<anonymous> (app.8761343d.js:1:4224)
    at i (app.8761343d.js:1:1294)
```

Instead of:

```typescript
TypeError: Cannot read properties of undefined (reading 'foo')
    at fetchData (src/utils/api.ts:15:10)
    at handleClick (src/components/Button.tsx:34:5)
```

Without more readable stack traces, it’s challenging to pinpoint the source of the error. This problem commonly affects TypeScript and JavaScript developers in scenarios like:

- Large-scale React applications bundled with tools like Vite or Webpack.
- TypeScript projects where the output is heavily transpiled.
- Debugging production builds during QA or user error reporting.

## Solution

Readable stack traces can be achieved by using **source maps**—a tool that maps your bundled code back to the original source code. Below are two approaches to enabling and troubleshooting stack traces for debugging:


### **Enabling Source Maps in Vite**

Vite can generate source maps during the build or development process, making stack traces point to original TypeScript and JavaScript files.

Enable source maps in Vite's configuration:
   Modify your `vite.config.ts` to include the following setting:

   ```typescript
   import { defineConfig } from 'vite';
   import react from '@vitejs/plugin-react';

   export default defineConfig({
     plugins: [react()],
     build: {
       sourcemap: true, // Generate source maps for builds
     },
     server: {
       sourcemap: true, // Enable in local development
     },
   });
   ```

Rebuild the project and ensure the browser has access to `.map` files:
   After enabling source maps, rebuild your application. In development, tools like Chrome DevTools will automatically pick up the corresponding `.map` files, allowing you to debug using the original code.

Check the outputs:
   Open the developer tools in the browser, reproduce the error, and see if the stack trace now references your original files.

- Ensure `.map` files are not deployed to production if they contain sensitive information.
- This approach works well for local environments or testing but requires additional configuration to share source maps with monitoring tools in production.

### **Configuring Source Maps in Webpack**

Webpack also supports generating source maps for debugging purposes. To enable them:

1. Modify the Webpack configuration:
   Update the `webpack.config.js` file to include the following:

   ```javascript
   module.exports = {
     mode: 'development', // Ensure you’re in development mode
     devtool: 'source-map', // Provides high-quality source maps
     module: {
       rules: [
         {
           test: /\.ts$/,
           use: 'ts-loader',
           exclude: /node_modules/,
         },
       ],
     },
   };
   ```

2. Serve the project:
   Start your development server (`webpack-dev-server`) and replicate the error. The stack trace should now point to the original source TypeScript or JavaScript files.

- The `devtool` property can take several values (`source-map`, `inline-source-map`, etc.). Each has trade-offs in terms of build time and map quality—choose the one that fits your workflow.
- Larger projects may see increased build sizes or slower development builds when generating source maps.

### Further Considerations

1. **Performance:**  
   Generating source maps can increase build time and potentially affect bundler performance. Use simpler source mapping approaches (e.g., `eval-source-map`) in local environments if build speed is a concern.

2. **Security:**  
   Source maps should not expose sensitive code in production environments. To address this:
   - Use tools like [Sentry](https://sentry.io/) to upload source maps privately for production debugging.
   - Configure the bundler to generate source maps only in development or testing environments.

3. **Alternative Debugging Methods:**  
   - Add meaningful logging and error-handling mechanisms to track the source of errors directly.
   - Use monitoring and reporting libraries that provide contextual information about errors (e.g., stack traces, user actions).

4. **Production Builds:**  
   If debugging issues occur only in production, tools like Sentry can help you correlate stack traces with uploaded source maps to locate the bugs.

## Related Resources

Here are some recommended readings and tools for further exploration:

1. [Vite Official Documentation - Source Maps](https://vitejs.dev/guide/build.html#source-code-mapping)
2. [Webpack Devtool Configuration Guide](https://webpack.js.org/configuration/devtool/)
3. [Understanding Source Maps](https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map)
4. [Sentry’s Guide to Using Source Maps](https://docs.sentry.io/platforms/javascript/sourcemaps/)
