如何为我的项目目录中的两个不同文件夹运行一个“npm start”

Jsc*_*mer 3 node.js npm reactjs react-scripts

我想运行一个npm start命令来运行我的前端文件夹和后端文件夹。目前,我必须在每个文件夹内导航并分别在两个文件夹上运行命令以查看我的应用程序在本地主机上打开。我已经“同时”查看了包,但是在我的 package.json 文件中实现它时遇到了一些问题。这是我的前端文件夹的 package.json 文件:

"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "react-snap"
Run Code Online (Sandbox Code Playgroud)

}

在前端运行 npm start 后,我​​尝试使用此位运行后端,但它不起作用:

"start": "set HOST = 'http://localhost' && set PORT=8000 && ../backend/ start & react-scripts start"
Run Code Online (Sandbox Code Playgroud)

编辑:重试后仍有一些麻烦。

这是我的 package.json 文件中的脚本

前端文件夹

"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject",
"postbuild": "react-snap"
},
Run Code Online (Sandbox Code Playgroud)

后端文件夹

"scripts": {
"start": "node ./bin/www"
},
Run Code Online (Sandbox Code Playgroud)

前端用 React 编写,后端用 Node.js 编写。任何帮助将不胜感激!

小智 8

我遇到了同样的问题,但现在已经解决了。假设服务器位于“后端”文件夹中,前端位于“客户端”文件夹中。

那么服务器端 package.json 中的脚本应该是这样的:

"scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "cd ../ && npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
Run Code Online (Sandbox Code Playgroud)

客户端 package.json 如下:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",
Run Code Online (Sandbox Code Playgroud)

然后“npm run dev”同时运行两个服务器


Cel*_*ton 5

在 package.json 文件夹(后端)中,转到脚本

然后添加这个:

"start": "node index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run start\" \"npm run client\"" 
Run Code Online (Sandbox Code Playgroud)

(假设您的客户端文件夹包含前端服务器)

然后,如果你想同时运行两台服务器,只需输入 npm run dev 就可以了