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

React.js使用Vite创建项目后运行npm run dev提示脚本缺失求助

解决Vite React项目npm run dev脚本缺失问题

检查package.json脚本配置

打开项目根目录的package.json,确认scripts字段是否包含以下内容:

"scripts": {
  "dev": "vite",
  "build": "vite build",
  "preview": "vite preview"
}

如果没有dev脚本,手动添加上述代码即可。

重新初始化Vite项目(若package.json损坏)

如果package.json丢失或结构异常,删除项目下的node_modules、package-lock.json(或yarn.lock),重新执行创建命令:

# npm
npm create vite@latest my-react-app -- --template react
# yarn
yarn create vite my-react-app --template react
# pnpm
pnpm create vite my-react-app -- --template react

进入新创建的项目目录,执行npm install后再运行npm run dev。

卸载不必要的依赖

你安装的nodemon和react-scripts对Vite项目无作用:

  • react-scripts是Create React App专属依赖,与Vite不兼容
  • Vite自带热更新,无需nodemon监听文件变化
    执行命令卸载:
npm uninstall nodemon react-scripts

清除缓存并重装依赖

若以上步骤无效,尝试清除npm缓存并重新安装依赖:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

内容的提问来源于stack exchange,提问作者Charu Jain

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 00:04:52