使用pnpm启动Vite+React项目后按CTRL+C退出出现ELIFECYCLE错误求助
Vite React项目使用pnpm退出时出现ELIFECYCLE错误
问题详情
用Vite创建React项目后,浏览器运行正常,但通过pnpm dev启动服务,按下CTRL+C退出时会触发ELIFECYCLE Command failed with exit code 1错误。使用npm run dev执行相同退出操作则无此问题,已安装对应版本pnpm,问题仍未解决。
执行命令及输出
pnpm install 输出
$ pnpm install Packages: +244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Packages are hard linked from the content-addressable store to the virtual store. Content-addressable store is at: C:\Users\MarionPro\AppData\Local\pnpm\store\v3 Virtual store is at: node_modules/.pnpm Progress: resolved 266, reused 244, downloaded 0, added 244, done Done in 14.8s
项目文件列表
$ ls index.html node_modules/ package.json pnpm-lock.yaml public/ README.md src/ vite.config.js
pnpm dev 输出
$ pnpm dev
my-portfolio@0.0.0 dev C:\Users\MarionPro\OneDrive\Documents\PORTFOLIO\my-portfolio
vite
VITE v4.4.5 ready in 1360 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h to show help
ELIFECYCLE Command failed with exit code 1.
依赖版本
生产依赖
- react 18.2.0
- react-dom 18.2.0
开发依赖
- @types/react 18.2.15 (18.2.20 可用)
- @types/react-dom 18.2.7
- @vitejs/plugin-react 4.0.3 (4.0.4 可用)
- eslint 8.45.0 (8.47.0 可用)
- eslint-plugin-react 7.32.2 (7.33.1 可用)
- eslint-plugin-react-hooks 4.6.0
- eslint-plugin-react-refresh 0.4.3
- vite 4.4.5 (4.4.9 可用)
配置文件内容
package.json
{ "name": "my-portfolio", "private": true, "version": "0.0.0", "type": "module", "scripts": { "dev": "vite", "build": "vite build", "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }, "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0" }, "devDependencies": { "@types/react": "^18.2.15", "@types/react-dom": "^18.2.7", "@vitejs/plugin-react": "^4.0.3", "eslint": "^8.45.0", "eslint-plugin-react": "^7.32.2", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.3", "vite": "^4.4.5" } }
vite.config.js
import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], });
解决方法
- 升级pnpm到最新稳定版:执行
pnpm add -g pnpm@latest,旧版本pnpm存在进程信号处理的兼容性问题,升级后大概率能解决。 - 修改dev脚本容错:在package.json中将dev命令修改为:
强制脚本以0状态码退出,忽略Vite被中断时的非零退出码。"dev": "vite; exit 0" - 添加pnpm钩子自动处理:在项目根目录创建
.pnpmfile.cjs文件,写入以下内容:
该钩子会自动给dev脚本添加容错逻辑,避免非零退出码触发ELIFECYCLE错误。module.exports = { hooks: { readPackage(pkg) { if (pkg.scripts?.dev) { pkg.scripts.dev = `${pkg.scripts.dev} || true`; } return pkg; }, }, }; - 升级Vite版本:当前使用的vite 4.4.5存在进程退出信号处理的已知问题,执行
pnpm update vite升级到最新可用版本(如4.4.9)即可修复。
内容的提问来源于stack exchange,提问作者MarionB
相关产品推荐
相关产品推荐

