React项目无法禁用Source Maps,配置修改后问题仍未解决
Hey there, let's work through these two problems step by step!
1. Getting Source Maps Properly Disabled
It sounds like your build config changes aren't taking effect—here are targeted fixes based on common frameworks:
For Vite projects:
Open yourvite.config.js/vite.config.tsand ensure this setting is present:export default defineConfig({ build: { sourcemap: false, // Disables source maps for production builds }, })For Create React App (CRA):
Add this line to your.envor.env.productionfile:GENERATE_SOURCEMAP=falseDon’t forget to restart your dev server if testing locally, and always rebuild with
npm run buildafter updating this setting.For Vue CLI projects:
Update yourvue.config.jswith this configuration:module.exports = { productionSourceMap: false, // Turns off source maps in production builds }Post-config verification steps:
- Delete your entire
dist(or build output) folder before rebuilding—old source map files might still linger and get deployed. - After building, check the output folder for any
.mapfiles. If they’re still there, double-check your config for typos (like mixing upsourcemapvssourceMapin some frameworks). - Confirm your deployment process isn’t uploading leftover
.mapfiles. Many hosting platforms deploy all files in the build folder, so make sure these files are gone before pushing changes.
- Delete your entire
2. Making Git Track Your .env File
If your .env file is showing as untracked, it’s almost certainly because it’s listed in your .gitignore file. Here’s how to resolve this:
- First, check your
.gitignorefor lines like.env,.env.local, or.env.*—these rules tell Git to ignore these files by default. - If you need to track a specific
.envfile (like.env.production—never track files with sensitive keys like API secrets!), add an exception to.gitignore:# Ignore most .env files by default .env .env.local # Allow tracking .env.production !.env.production - To confirm which rule is ignoring your file, run this terminal command:
It will show you exactly which line ingit check-ignore -v .env.gitignoreis causing the file to be skipped. - Finally, stage the file with Git:
You should now be able to commit it normally.git add .env
A quick reminder: Only commit .env files that don’t contain sensitive information (like API keys or database passwords). For local development, use .env.local which should stay in .gitignore to keep secrets safe.
内容的提问来源于stack exchange,提问作者Yahia Naguib

