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
相关产品推荐
相关产品推荐

