[英]How to run node server.js and npm start with one command
I spun up a new create-react-app and an Express backend locally.我在本地创建了一个新的 create-react-app 和一个 Express 后端。 Right now I have to run
node server.js
and npm start
separately to make sure both the frontend and backend runs.现在我必须分别运行
node server.js
和npm start
以确保前端和后端都运行。
Is there a way that I can run both with just one npm command by just editing my package.json file?有没有一种方法可以通过编辑我的 package.json 文件只用一个 npm 命令来运行两者?
In your package.json
add another script在你的
package.json
添加另一个脚本
"scripts": { "start": "..." "start-server": "node server.js && npm start", }
This is how I do it using a module named concurrently .这就是我使用名为concurrently的模块来做到这一点的方法。
Add the script to the package.json file of the root folder.将脚本添加到根文件夹的 package.json 文件中。
"scripts": { “脚本”:{
"test": "echo \\"Error: no test specified\\" && exit 1", "test": "echo \\"Error: no test specified\\" && exit 1",
"start": "node index.js", "start": "节点 index.js",
"client": "npm run start --prefix client", "client": "npm run start --prefix client",
"server": "nodemon index.js", "server": "nodemon index.js",
"dev": "concurrently \\"npm run client\\" \\"npm run server\\"" "dev": "同时\\"npm run client\\" \\"npm run server\\""
} }
Yes you can.是的你可以。 Under your package.json file you can use:
在您的 package.json 文件下,您可以使用:
{
"name": "projectX",
"version": "1.0.0",
"scripts": {
"dev:api": "node server.js",
"dev:client": "npm start",
"dev": "npm run dev:api && npm run dev:client"
}
}
If you run npm run dev
it will run both of your scripts.如果你运行
npm run dev
它会运行你的两个脚本。
But I wouldn't recommend this approach because you are making your backend & frontend dependent on each other.但我不会推荐这种方法,因为您让后端和前端相互依赖。 That means you will have one version for both, one CI/CD pipeline, one deployment.
这意味着您将拥有一个版本,一个 CI/CD 管道,一个部署。
I would have two separate projects.我会有两个独立的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.