使用create-react-app构建生产环境失败:代码压缩报错求助
Hey there, let's work through this frustrating build error you're hitting. It's super common for create-react-app projects to run smoothly in dev mode (npm start) but fail during production builds—usually because the production compiler is way stricter about syntax and code validity. Let's break down the most likely fixes:
Common Causes & Solutions
Check for syntax errors or incomplete code in Indecision.js
Your code snippet shows a truncated import:import AddOptions from './AddOptio...—first, make sure this line is complete! Did you accidentally cut off the filename (likeAddOptions.js)? Even small typos in import paths, unclosed JSX tags, missing semicolons, or undefined variables can slip past dev mode but break the production minifier. Go through Indecision.js line by line to spot these tiny oversights.Verify you're not using untranspiled ES6+ syntax
While create-react-app supports most modern JS features out of the box, some edge cases (like experimental syntax or misconfigured class properties) can trip up the Terser minifier used in production. For example, if you're using arrow functions directly in class fields without proper setup, or optional chaining/Nullish Coalescing in older versions of react-scripts, this could cause a minify failure. Try updating react-scripts to the latest version first:npm install react-scripts@latestClear cached dependencies and build artifacts
Corrupted node_modules or cached build files often cause weird, unexplainable build issues. Run these commands (adjust for Windows if needed):rm -rf node_modules build package-lock.json npm cache clean --force npm install npm run buildCheck for unused variables or dead code that's confusing the minifier
Sometimes the production compiler flags unused variables or unreachable code as errors, even if dev mode ignores them. Use ESLint (which comes pre-configured with create-react-app) to scan for these:npm run lintFix any errors it reports, then try building again.
If none of these work, sharing more of your Indecision.js code (especially around the parts that might be triggering the minify error) would help narrow things down further!
内容的提问来源于stack exchange,提问作者Ali Khalil

