安装react-router-dom后npm脚本异常,项目模块丢失无法启动
Hey there, sorry to hear you're stuck with this frustrating issue—losing scripts and modules after a package install is the worst. Let's work through some concrete steps to get your project back on track:
1. Wipe Clean Dependencies and Cache
First, let's eliminate any corrupted files or cached data that might be causing the issue:
- Delete your
node_modulesfolder and lock files with these commands:- For macOS/Linux:
rm -rf node_modules package-lock.json yarn.lock - For Windows Command Prompt:
rmdir /s /q node_modules && del package-lock.json yarn.lock
- For macOS/Linux:
- Clear the npm cache to remove any stale packages:
npm cache clean --force
2. Restore Missing Scripts in package.json
If your package.json lost all its scripts entries, manually add back the default Create React App scripts (adjust if you're using a custom setup):
"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }
After adding these, re-install all dependencies:npm install
Then re-add react-router-dom explicitly:npm install react-router-dom
3. Fix Global vs. Local React Scripts Conflicts
Sometimes global react-scripts installations can interfere with local project dependencies:
- Uninstall the global version first:
npm uninstall -g react-scripts - Reinstall
react-scriptslocally to your project:npm install react-scripts --save
4. Verify Core Project Files
Double-check that critical project files haven't been accidentally deleted:
- Ensure
public/index.htmlexists (this is required for the dev server to run) - Confirm
src/index.jsis present and has the basic React render setup
If any core files are missing, it's often faster to generate a fresh Create React App project and migrate your code over, rather than rebuilding files manually.
5. Check Node.js/NPM Version Compatibility
Outdated or bleeding-edge versions of Node.js or npm can cause unexpected dependency issues. Try switching to a stable LTS version (like Node.js 18.x):
- If you use
nvm, run:nvm install 18 && nvm use 18 - Then repeat the dependency install steps above.
Once you've worked through these steps, try running npm start again—this should resolve the majority of cases where scripts and modules go missing after a package install.
内容的提问来源于stack exchange,提问作者Jay Jeong

