You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装react-router-dom后npm脚本异常,项目模块丢失无法启动

Fixing React Project Breakage After Installing react-router-dom

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_modules folder 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
  • 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-scripts locally 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.html exists (this is required for the dev server to run)
  • Confirm src/index.js is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:17:47