Course contentsShow
← Web Development
Lesson 1395 of 2,98225. Build Tools and BundlersFree lesson

Source Maps for Debugging

Configuring devtool options to generate source maps that map bundled code back to original source.

Source Maps for Debugging

You'll learn how to configure Webpack's devtool option to generate source maps that bridge the gap between your bundled code and original source files.

Why Source Maps Matter

When Webpack bundles your code, it transforms, minifies, and combines multiple files into optimized output. This is great for performance but terrible for debugging—error messages point to line 1, column 8472 of a minified bundle instead of your actual source file.

Source maps solve this by creating a reference file that maps positions in the bundled code back to your original source files. Think of it like a translator that tells your browser's DevTools "this minified line actually came from Header.js, line 42."

Configuring the devtool Option

In your webpack.config.js, the devtool property controls source map generation:

module.exports = {
  mode: 'development',
  devtool: 'source-map', // Full source maps
  entry: './src/index.js',
  output: {
    filename: 'bundle.js'
  }
};

Common devtool Values

  • 'source-map': Full, separate source map files (.map). Slowest build, best quality debugging.
  • 'eval-source-map': Inline source maps with eval. Fast rebuilds, good for development.
  • 'cheap-source-map': Faster generation, column mappings omitted, loader source maps ignored.
  • 'inline-source-map': Source map embedded as DataURL in bundle (larger file size).
  • false: No source maps (production default when unspecified).

For development with Webpack Dev Server, 'eval-source-map' or 'cheap-module-source-map' offer good speed/quality balance. For production, use 'source-map' or omit it entirely.

Key Takeaway: The devtool option in webpack.config.js generates source maps that let you debug your original source code instead of wrestling with bundled, transformed output—essential for productive development workflows.