'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. Right now I have to run node server.js and npm start separately to make sure both the frontend and backend runs.
Is there a way that I can run both with just one npm command by just editing my package.json file?
Solution 1:[1]
This is how I do it using a module named concurrently.
- Install concurrently using npm.
Add the script to the package.json file of the root folder.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"client": "npm run start --prefix client",
"server": "nodemon index.js",
"dev": "concurrently \"npm run client\" \"npm run server\""
}
Solution 2:[2]
In your package.json add another script
"scripts": {
"start": "..."
"start-server": "node server.js && npm start",
}
Solution 3:[3]
Yes you can. Under your package.json file you can use:
{
"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.
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.
I would have two separate projects.
Solution 4:[4]
In your package.json file just add this script. There is no need for an npm package. However, if you need one concurrently will do the trick.
"scripts": {
"client": "npm run start --prefix client",
"server": "npm run watch --prefix server",
"watch": "npm run server & npm run client",
"test": "echo \"Error: no test specified\" && exit 1"
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rudresh solanki |
| Solution 2 | Dibakar Halder |
| Solution 3 | DonCarleone |
| Solution 4 | gajuki |
