安装React后执行npm start报错:npm ERR! missing script: start
Hey there, let’s get that annoying error sorted out so you can launch your React app. This is one of the most common hiccups when starting out with React, and here are the step-by-step fixes that usually resolve it:
Double-check your working directory
First off, make sure you’re runningnpm startfrom the root folder of your React project. If you created your app withnpx create-react-app my-app, you need tocd my-appfirst before running the start command. Usepwd(Mac/Linux) orcd(Windows) to confirm you’re in the right place—you should see thepackage.jsonfile in this folder.Verify the
startscript exists inpackage.json
Open up your project’spackage.jsonfile and look for thescriptssection. A standard React app should have this line:"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }If the
startline is missing, add it manually and save the file. This is often the case if the project initialization was interrupted or you set up the project manually instead of usingcreate-react-app.Reinstall your dependencies
Sometimes corrupted node modules or lock files cause this issue. Try these steps:- Delete the
node_modulesfolder andpackage-lock.json(oryarn.lockif you use Yarn) - Run
npm installto reinstall all dependencies fresh
Here are the commands for different systems:
# Mac/Linux rm -rf node_modules package-lock.json npm install # Windows (Command Prompt) rmdir /s /q node_modules del package-lock.json npm install- Delete the
Ensure your React project was created correctly
If you didn’t usecreate-react-appto set up your project, you might not have the pre-configuredstartscript. In this case, either:- Use
npx create-react-app my-new-appto generate a standard React project with all necessary scripts, or - Configure the
startscript yourself (you’ll need to set up a bundler like Webpack and a dev server manually, which is more advanced)
- Use
Try running the script directly with
npx
If all else fails, skip the npm script and run the React dev server directly:npx react-scripts startYou can also try updating your npm version to the latest with
npm install -g npm@latestto rule out any version-related bugs.
内容的提问来源于stack exchange,提问作者Gagandeep

