执行ng build --prod命令构建Angular项目时遭遇"Invalid version: '15.2-15.3'"错误的技术求助
Hey there, I’ve dealt with this exact error when running ng build --prod before—let’s get your project building again!
What’s Causing This?
This error stems from esbuild (the tool Angular uses under the hood for production builds) refusing to parse a version string that doesn’t follow Semantic Versioning (SemVer) rules. The string "15.2-15.3" is invalid because SemVer requires versions to use dots for patches/minors and hyphens only for pre-release tags (like 15.2.0-beta.1).
This bad version string is almost certainly hiding in either:
- Your project’s own
package.json(check theversionorenginesfields) - A third-party dependency’s
package.jsonfile
Step-by-Step Fixes
1. Find the Problematic Package
First, track down which package is using the invalid version:
- Run
npm ls --allin your project root, then search the output for"15.2-15.3"to spot the culprit. - If you use Yarn, run
yarn why <package-name>once you have a suspect to confirm its origin. - You can also manually check
node_modulesfolders for packages with apackage.jsoncontaining the bad version string.
2. Fix the Version Issue
Depending on where the problem is:
- Your project’s package.json: Update the invalid version string to follow SemVer. For example, replace
"15.2-15.3"with">=15.2.0 <15.4.0"if you’re defining a range, or a specific version like"15.2.0". - A third-party dependency:
- Try updating the dependency to its latest version—chances are the maintainer fixed the version formatting. Run
npm update <package-name>to do this. - If updating isn’t an option, use npm’s
overrides(or Yarn’sresolutions) to force a valid version. Add this to your project’spackage.json:"overrides": { "problematic-package-name": "15.2.0" } - If the package isn’t essential, simply remove it with
npm uninstall <package-name>.
- Try updating the dependency to its latest version—chances are the maintainer fixed the version formatting. Run
3. Clean Cache and Rebuild
Once you’ve fixed the version issue, clear any cached data to ensure no old versions linger:
- Run
npm cache clean --force(oryarn cache cleanfor Yarn) - Delete your
node_modulesfolder andpackage-lock.json(oryarn.lock) - Reinstall dependencies with
npm install - Try building again with
ng build --prod
Bonus: Check Angular DevKit Compatibility
If the issue persists, make sure your @angular-devkit/build-angular version is compatible with your Angular core version. Mismatches here can sometimes trigger odd esbuild errors. Update it to a version that matches your Angular major/minor version with:
npm install @angular-devkit/build-angular@<your-angular-version>
内容的提问来源于stack exchange,提问作者omar mohsen

