You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Windows环境下package.json脚本报错:缓存清理命令执行失败

我来帮你解决这个Windows下rmdir命令的问题,同时也给你梳理下端口配置的优化方案:

问题根源

Windows自带的rmdir(或等价的rd)命令不支持一次性传入多个目录路径,你在clean脚本里写了rmdir /s/q .cache client/dist,系统会把第二个目录client/dist误判为命令的无效开关参数,所以抛出了Invalid switch - "dist"的错误。

解决方案

有两种实用的修复方式,你可以根据场景选择:

方式1:拆分rmdir命令(无需额外依赖)

把两个目录的删除操作拆成独立的rmdir命令,用&&连接(确保前一个命令执行成功后才会执行下一个):

"scripts": {
  "test": "jest --watchAll",
  "start": "npm run build-client && node index.js",
  "dev": "npm run dev-client & nodemon index.js",
  "dev-peer": "cross-env GENERATE_PEER_PORT='true' nodemon index.js",
  "build-client": "npm run clean && parcel build client/src/index.html --out-dir client/dist",
  "dev-client": "npm run clean && parcel client/src/index.html --out-dir client/dist",
  "clean": "rmdir /s/q .cache && rmdir /s/q client/dist"
}

方式2:使用跨平台工具rimraf(推荐)

如果你的项目需要在Windows和类Unix系统(Mac/Linux)上兼容运行,推荐用rimraf——它是rm -rf的跨平台替代工具,支持一次性删除多个目录,完全不用纠结系统命令差异:

  1. 先安装rimraf作为开发依赖:
npm install --save-dev rimraf
  1. 修改clean脚本:
"scripts": {
  "test": "jest --watchAll",
  "start": "npm run build-client && node index.js",
  "dev": "npm run dev-client & nodemon index.js",
  "dev-peer": "cross-env GENERATE_PEER_PORT='true' nodemon index.js",
  "build-client": "npm run clean && parcel build client/src/index.html --out-dir client/dist",
  "dev-client": "npm run clean && parcel client/src/index.html --out-dir client/dist --port 3000",
  "clean": "rimraf .cache client/dist"
}
端口配置优化

针对你提到的「3000端口运行React前端、1234端口运行后端」的需求,补充两个小调整:

  • 前端:在dev-client脚本里添加--port 3000参数,让Parcel在指定端口启动服务(如上代码所示)
  • 后端:确保你的index.js里把服务端口配置为1234,比如:
const express = require('express');
const app = express();
const PORT = 1234;

app.listen(PORT, () => {
  console.log(`Backend running on port ${PORT}`);
});

内容的提问来源于stack exchange,提问作者s.collins

火山引擎 最新活动