Node.js React项目package.json构建脚本执行失败(JSON语法错误)
Hey there, let's get this build error sorted out—this is almost always a JSON syntax mistake in your package.json file, which both Yarn and npm can't parse correctly. Here's how to fix it step by step:
1. Locate and fix the JSON syntax error
The error points directly to position 175 in your package.json, so start by opening the file and checking that area. Common culprits include:
- A trailing comma after the last key-value pair in an object (JSON doesn't allow these!)
- Mismatched curly braces (
{/}) or quotes (make sure all strings are wrapped in straight double quotes) - A missing comma between key-value pairs
To make this easier:
- Use a code editor like VS Code, which will highlight JSON syntax errors in red automatically.
- Run this command in your project folder to get a detailed breakdown of the error:
Node.js will try to load yournode -e "require('./package.json')"package.jsonand spit out a precise error message about what's wrong.
For example, if you have something like this (note the trailing comma after "build"):
"scripts": { "start": "react-scripts start", "build": "react-scripts build", }
You'll need to remove that extra comma to fix the syntax.
2. Verify the fix
Once you've corrected the syntax, test it first with npm (since you mentioned it gives more detailed errors):
npm run build
If this works, the JSON issue is resolved. You can then try running yarn build again—it should now succeed too.
3. Clean package manager caches (if needed)
If you still see errors after fixing the JSON, the cache might be holding onto a corrupted version of your package.json:
- For Yarn:
yarn cache clean - For npm:
npm cache clean --force
After cleaning, re-run the build command.
4. Check for hidden issues (rare)
If all else fails, make sure your package.json is saved in UTF-8 encoding (no weird special characters or full-width punctuation marks) and that there are no invisible whitespace characters messing up the parsing.
内容的提问来源于stack exchange,提问作者Nompumelelo

