How to Debug TypeScript in Chrome
In this article
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, orhttp-server) - TypeScript compiler (
tsc) installed - Source maps enabled in your build process
Step-by-Step
- Enable source maps in tsconfig.json: Open your
tsconfig.jsonfile and ensure the"sourceMap"option is set totrue. This generates.js.mapfiles 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. - Compile your TypeScript code: Run
tscin your project root. This compiles.tsfiles into.jsfiles in your output directory (e.g.,dist), along with corresponding.js.mapfiles. Verify that both.jsand.js.mapfiles are present. - Serve your project locally: Use a local server to serve your compiled files. For example, install
http-serverglobally via npm (npm install -g http-server) and runhttp-server dist(or your output directory). This ensures files are served with proper MIME types and avoids CORS issues when loading source maps. - 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. - 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
.tsfiles. - Locate your TypeScript files: In the Sources panel, expand the relevant folder (e.g.,
webpack:// > . > src) to find your.tsfiles. If you don’t see them, check that source maps were generated and are being loaded (see Troubleshooting). - 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.
- 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.
- 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.
- 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. - 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
sourceMapintsconfig.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— incorrectrootDiroroutDircan break source map resolution. - Ignoring CORS or MIME type issues when loading source maps — ensure your server sends correct headers.
Tips
- Use
inlineSourceMap: trueintsconfig.jsonto embed source maps directly in JavaScript files, avoiding separate.mapfiles. - Name your output directory clearly (e.g.,
dist) and keep it in sync withoutDirintsconfig.json. - Use
console.logsparingly; breakpoints are more efficient for inspecting state. - Enable
"inlineSources": trueto include original TypeScript source in the source map, useful when files aren’t served. - Automate compilation with
tsc --watchto 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. Checktsconfig.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 withtsc 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.