User Agents

Web browsers and everyday web tech, explained

How to Debug TypeScript in Chrome

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Debugging TypeScript in Chrome allows developers to inspect, step through, and fix issues in their TypeScript code directly in the browser, even though Chrome runs JavaScript. This is achieved by using source maps, which link the compiled JavaScript back to the original TypeScript files. With proper configuration, you can set breakpoints, inspect variables, and trace execution flow as if you were debugging the TypeScript itself. This guide walks you through setting up and using Chrome DevTools to debug TypeScript effectively.

What You'll Need

  • A TypeScript project with tsconfig.json
  • Chrome browser (latest stable version recommended)
  • Chrome DevTools (built-in, accessible via F12 or right-click > Inspect)
  • A local development server (e.g., live-server, webpack-dev-server, or http-server)
  • TypeScript compiler (tsc) installed
  • Source maps enabled in your build process

Step-by-Step

  1. Enable source maps in tsconfig.json: Open your tsconfig.json file and ensure the "sourceMap" option is set to true. This generates .js.map files alongside compiled JavaScript. Example:
    {
      "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "./dist",
        "rootDir": "./src",
        "sourceMap": true
      }
    }
    Without source maps, Chrome cannot map JavaScript back to TypeScript.
  2. Compile your TypeScript code: Run tsc in your project root. This compiles .ts files into .js files in your output directory (e.g., dist), along with corresponding .js.map files. Verify that both .js and .js.map files are present.
  3. Serve your project locally: Use a local server to serve your compiled files. For example, install http-server globally via npm (npm install -g http-server) and run http-server dist (or your output directory). This ensures files are served with proper MIME types and avoids CORS issues when loading source maps.
  4. Open Chrome and navigate to your site: Visit http://localhost:8080 (or your server’s port). Make sure the page loads without console errors. If scripts fail to load, check file paths and server configuration.
  5. Open Chrome DevTools: Press F12 or right-click the page and select Inspect. Go to the Sources tab. You should see a list of loaded resources, including your JavaScript files and, if source maps are working, a webpack:// or file:// entry containing your original .ts files.
  6. Locate your TypeScript files: In the Sources panel, expand the relevant folder (e.g., webpack:// > . > src) to find your .ts files. If you don’t see them, check that source maps were generated and are being loaded (see Troubleshooting).
  7. Set breakpoints: Click on a line number in your TypeScript file within DevTools to set a breakpoint. A blue marker will appear. When the code executes and hits that line, execution will pause, allowing you to inspect the call stack, scope variables, and step through code.
  8. Trigger the breakpoint: Interact with your app (e.g., click a button, submit a form) to reach the code path containing the breakpoint. Execution will pause, and the Call Stack and Scope panels will show current state.
  9. Step through code: Use the step controls (Step Over, Step Into, Step Out) to navigate line by line. Watch how variables change and identify logic errors. You can also use the Console while paused to evaluate expressions in the current context.
  10. Use conditional breakpoints (optional): Right-click a line number and select Add conditional breakpoint. Enter a condition (e.g., i === 10) so the breakpoint only triggers when the condition is true. This is useful for debugging loops or rare conditions.
  11. Check the Console and Network tabs: While debugging, monitor the Console for errors and logs, and the Network tab for resource loading issues. Missing source maps often appear as 404s here.

Common Mistakes

  • Forgetting to enable sourceMap in tsconfig.json — without it, debugging TypeScript is impossible.
  • Serving files directly via file:// instead of a local server — this can block source map loading due to browser security policies.
  • Not rebuilding TypeScript after code changes — outdated JavaScript won’t reflect breakpoints set in current code.
  • Misconfigured paths in tsconfig.json — incorrect rootDir or outDir can break source map resolution.
  • Ignoring CORS or MIME type issues when loading source maps — ensure your server sends correct headers.

Tips

  • Use inlineSourceMap: true in tsconfig.json to embed source maps directly in JavaScript files, avoiding separate .map files.
  • Name your output directory clearly (e.g., dist) and keep it in sync with outDir in tsconfig.json.
  • Use console.log sparingly; breakpoints are more efficient for inspecting state.
  • Enable "inlineSources": true to include original TypeScript source in the source map, useful when files aren’t served.
  • Automate compilation with tsc --watch to rebuild on save.

Troubleshooting

If breakpoints aren’t hit, first verify that source maps are being generated and loaded. In Chrome DevTools, go to Sources > Page and look for your .js.map files. If they’re missing or show 404 in the Network tab, check file paths and server configuration. Ensure your server serves .map files with MIME type application/json. If TypeScript files don’t appear in the Sources panel, check sourceRoot in your tsconfig.json — it should point to your source directory. Clear browser cache and reload if changes aren’t reflected. Disable DevTools caching via the Network tab settings if needed. If using a bundler like Webpack, ensure its devtool option (e.g., source-map) is compatible with TypeScript source maps.

Frequently Asked Questions

Can I debug TypeScript without source maps?

No. Source maps are required to map JavaScript back to TypeScript. Without them, you can only debug the compiled JavaScript.

Why don’t I see my .ts files in Chrome DevTools?

This usually means source maps aren’t generated or loaded. Check tsconfig.json and verify .js.map files exist and are accessible via your server.

Do I need a bundler to debug TypeScript?

No. You can debug plain TypeScript compiled with tsc as long as source maps are enabled and files are served properly.

Can I debug TypeScript in other browsers?

Yes. Firefox, Edge, and Safari also support source maps and can debug TypeScript similarly.

Why does my breakpoint say ‘Unbound breakpoint’?

This means Chrome can’t map the line to valid code. Ensure the source map is correct, the file hasn’t changed since compilation, and the path matches exactly.

Should I commit .map files to version control?

Generally no. Source maps are build artifacts. Exclude them with .gitignore unless needed for production debugging.