React.js中执行npm start命令时遇到问题
Hey there! Let's troubleshoot that npm start error you're hitting after setting up your React app. I've dealt with this exact scenario dozens of times, so here are the most reliable fixes sorted by how likely they are to resolve the issue:
npm start Error in React App Clear Corrupted Dependencies & Cache
This is my go-to first step when React scripts act up. Corrupted packages or cached data are super common culprits:- Stop any running dev servers first
- Delete the
node_modulesfolder andpackage-lock.json(oryarn.lockif you use Yarn) from yourmy-appdirectory - Run
npm cache clean --forceto wipe npm's local cache - Reinstall dependencies with
npm install - Try
npm startagain
Ditch Global
create-react-app(UsenpxInstead)
The official React docs no longer recommend installingcreate-react-appglobally—this often causes version conflicts between the global package and the latest React scripts. Let's fix this:- Uninstall the global package:
npm uninstall -g create-react-app - Delete your existing
my-appfolder - Create a new app with the official, up-to-date method:
npx create-react-app my-app - Cd into
my-appand runnpm start
- Uninstall the global package:
Resolve Port Conflicts
By default,npm startuses port 3000. If another app (like a local API, another dev server, or even a background tool) is using that port, you'll get an error. Here's how to fix it:- Quick fix: When the error prompts you to run on a different port, just type
Yand hit enter - Manual port set: Modify the start script in
package.json:- For macOS/Linux: Change
"start": "react-scripts start"to"start": "PORT=3001 react-scripts start" - For Windows: Change it to
"start": "set PORT=3001 && react-scripts start"
- For macOS/Linux: Change
- Kill the conflicting process: On macOS/Linux run
lsof -ti:3000 | xargs kill -9; on Windows, open Task Manager, find the Node.js process using port 3000, and end it
- Quick fix: When the error prompts you to run on a different port, just type
Update Node.js & npm to Compatible Versions
Outdated Node.js or npm versions often clash with React's latest scripts. Here's what to do:- Check your current versions with
node -vandnpm -v - React recommends Node.js 14.0.0+ and npm 5.6+. If yours are older:
- Update Node.js to the latest stable version via your preferred package manager
- Update npm with
npm install -g npm@latest
- Check your current versions with
Fix Global Permission Issues
If you ran into errors during the globalcreate-react-appinstall, you probably have permission problems. Avoid usingsudo(it can mess up your npm setup long-term) — instead, stick with thenpxmethod above (it skips global installs entirely) or configure npm to use a local directory for global packages.
内容的提问来源于stack exchange,提问作者abu abu

