D.S
D.S

Reputation: 105

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?

Upvotes: 8

Views: 63565

Answers (4)

gajuki
gajuki

Reputation: 1

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"
      },

Upvotes: 0

hurricane
hurricane

Reputation: 6724

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.

Upvotes: 1

rudresh solanki
rudresh solanki

Reputation: 1015

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\""
    }

Upvotes: 7

Dibakar Halder
Dibakar Halder

Reputation: 213

In your package.json add another script

"scripts": {
    "start": "..."
    "start-server": "node server.js && npm start",
  }

Upvotes: 1

Related Questions