【问题标题】:Run two npm commands concurrently同时运行两个 npm 命令
【发布时间】:2023-04-03 21:40:01
【问题描述】:
我已经设置json-server 并在port-3000 上成功运行,然后运行npm start 它在其他端口3001 上运行。
但我想同时运行两者。我尝试使用Concurrently,但没有成功。
当我执行这个命令时:
$ concurrently "npm start" "json-server --watch ./topPanelData.json"
错误信息:
【问题讨论】:
标签:
reactjs
create-react-app
json-server
【解决方案1】:
通过这些步骤解决了问题:
-
使用以下密钥创建json-server.json,以在不同的端口上运行服务器。
{
"port": 4000
}
-
更新package.json中的启动脚本
"start": "concurrently \"react-scripts start\" \"json-server ./topPanelData.json\""
-
只需运行$ npm start 它会同时在不同的端口上执行
json-server: http://localhost:4000/topPanelData
React 应用: http://localhost:3000/
【解决方案2】:
您可以使用
杀死在端口
3000 中运行的进程
kill $(lsof -t -i:3000)
在哪里
lsof -t -i:3000
找到在 3000 端口上运行的进程并杀死该进程
如果您只在使用concurrently 时遇到此错误,则表示它正在尝试在端口3000 中运行这两个进程
尝试更改package.json中的启动脚本
"start": "export PORT=3006 react-scripts start"
通过一些实验,您将能够弄清楚。
【解决方案3】:
只需在您的项目目录中创建一个 .env 文件,与 .json 文件平行并添加以下行。
PORT=3001
这个解决方案对我有用。