How to Run Multiple NPM Scripts Using Concurrently
"client-install": "npm install --prefix client", "start": "node server.js", "server": "nodemon server.js", "client": "npm start --prefix client"
package.jsonfile is the one in the server directory and not the one in the client folder.
This would require you to open multiple terminal tabs and run
npm run client and
npm run server. Overtime, this can get tiring.
To save time, you can run both commands using a single script. All you need is to use a package called concurrently.
What is Concurrently?
Concurrently is an npm package that allows you to run multiple commands on your terminal concurrently.
How to use it?
To use concurrently, you need to install it via npm with the command:
npm install --save-dev concurrently
--save-dev flag will tell npm to install it as a devDependency.
To then add it as an npm script, you will add it in your
"script-name": concurrently "one process" "another process"
Using the example above, in order to use concurrently to start up our React application and Node server, you will need to add a script in the server’s
"client-install": "npm install --prefix client", "start": "node server.js", "server": "nodemon server.js", "client": "npm start --prefix client", "dev": "concurrently \"npm run server\" \"npm run client\""
Now when you run
npm run dev in the terminal, it will start both the React app and Node server for you.
For more information, check out the official docs.
If this was helpful, interesting, or caused some other positive emotion, please share!