π§ How to Disable Source Maps in Vite + React (and Why You Might Want To)
When you build a React app with Vite, it automatically generates source maps β files that let your browser map compiled code (like index-abcd123.js) back to your original React source (App.jsx).
This is great for debugging, but not always ideal for production builds, where you may want to:
Letβs explore how to control source map generation in a Vite + React project, with examples and real behavior differences.
π§© What Are Source Maps?
When Vite bundles your React code, it compiles and minifies everything into optimized JS files like this:
function App(){console.log("Button clicked!")}export{App as default};
Thatβs unreadable β but your browserβs DevTools can use a .map file (like index-abcd123.js.map) to map errors and console logs back to your original JSX source.
Without source maps, errors in production will point to something like:
index-abcd123.js:1:10243
With source maps enabled, they show:
App.jsx:5:11
βοΈ Controlling Source Maps in Vite
Open your vite.config.js file and configure the build.sourcemap option.
β Example: Disable Source Maps in Production
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig(({ mode }) => {
const isProduction = mode === 'production'
return {
plugins: [react()],
build: {
sourcemap: !isProduction, // β
Enabled in dev, disabled in prod
},
}
})
Explanation:
In development (
npm run dev),sourcemapistrue, so you can debug easily.In production (
npm run build),sourcemapisfalse, so.mapfiles are not generated.
π§ͺ Example: Simple React Component
src/App.jsx
export default function App() {
const handleClick = () => {
console.log('Button clicked! π')
throw new Error('Demo error!')
}
return (
<div style={{ padding: 20 }}>
<h1>Hello Vite + React π</h1>
<button onClick={handleClick}>Click me</button>
</div>
)
}
π§° Development Mode (npm run dev)
In dev mode, Vite automatically enables source maps.
If you click the button and trigger an error, youβll see:
Error: Demo error!
at handleClick (App.jsx:5:11)
You can open DevTools β Sources tab, and see the actual src/App.jsx file, just like your editor.
β Perfect for debugging.
π Production Mode (npm run build + vite preview)
In production mode, since sourcemap: false, your dist/ folder will look like:
dist/
βββ assets/
β βββ index-xyz123.js
β βββ index-xyz123.css
βββ index.html
No .map files appear.
Now, when an error happens in production, your browser shows:
Error: Demo error!
at Object.onclick (index-xyz123.js:1:17245)
No mention of App.jsx β just the minified output.
That means your original source isnβt exposed.
π‘οΈ Why Disable Source Maps in Production?
Protect your source code
.mapfiles often include your original JSX, comments, and variable names β valuable intellectual property.Reduce build size
.mapfiles can be large (sometimes larger than your JS bundle).Improve security
Prevents attackers from reverse-engineering your logic directly from production builds.
βοΈ Optional: Hide Source, Keep Trace Info
If you still want debugging info but donβt want to expose source content:
build: {
sourcemap: true,
sourcemapExcludeSources: true,
}
This keeps .map files but excludes your original code β so errors still have line numbers, but no readable source.
π Quick Summary
| Mode | Command | Sourcemap | Behavior |
| Development | npm run dev | β Yes | Easy debugging |
| Production | npm run build | β No | Secure, optimized build |
| Optional | β | βοΈ sourcemapExcludeSources: true | Keeps maps, hides code |
π¬ Final Thoughts
Disabling source maps in production is a small but powerful optimization β your build gets smaller, safer, and faster.
Meanwhile, you still keep full debugging comfort during development.
For most React projects using Vite, the snippet below is all you need:
build: { sourcemap: process.env.NODE_ENV !== 'production' }



