7

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?

D.S
  • 95
  • 1
  • 1
  • 2
  • Is it an option to just create a batch/shell file of some kind and execute both commands? – DesperateEi Dec 02 '19 at 11:18
  • I am not sure if it works with react too, but have you looked at https://nodemon.io/? – Aniket Kariya Dec 02 '19 at 11:20
  • Does this answer your question? [How can I run multiple npm scripts in parallel?](https://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel) – Mrkinix Dec 02 '19 at 11:24
  • I would recomend this package: https://www.npmjs.com/package/concurrently – Peter Dec 02 '19 at 11:25

4 Answers4

6

This is how I do it using a module named concurrently.

  1. Install concurrently using npm.
  2. 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\""
    }

rudresh solanki
  • 925
  • 9
  • 4
1

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.

DonCarleone
  • 544
  • 11
  • 20
hurricane
  • 6,521
  • 2
  • 34
  • 44
1

In your package.json add another script

"scripts": {
    "start": "..."
    "start-server": "node server.js && npm start",
  }
Dibakar Halder
  • 203
  • 3
  • 8
0

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"
      },
gajuki
  • 1