简体   繁体   中英

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?

In your package.json add another script

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM