How can I run multiple npm scripts in parallel?

后端 未结 22 2113
灰色年华
灰色年华 2020-11-22 05:48

In my package.json I have these two scripts:

  \"scripts\": {
    \"start-watch\": \"nodemon run-babel index.js\",
    \"wp-server\": \"webpack-         


        
相关标签:
22条回答
  • 2020-11-22 06:14

    I have been using npm-run-all for some time, but I never got along with it, because the output of the command in watch mode doesn't work well together. For example, if I start create-react-app and jest in watch mode, I will only be able to see the output from the last command I ran. So most of the time, I was running all my commands manually...

    This is why, I implement my own lib, run-screen. It still very young project (from yesterday :p ) but it might be worth to look at it, in your case it would be:

    run-screen "npm run start-watch" "npm run wp-server"
    

    Then you press the numeric key 1 to see the output of wp-server and press 0 to see the output of start-watch.

    0 讨论(0)
  • 2020-11-22 06:15

    Use a package called concurrently.

    npm i concurrently --save-dev

    Then setup your npm run dev task as so:

    "dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
    
    0 讨论(0)
  • 2020-11-22 06:17

    From windows cmd you can use start:

    "dev": "start npm run start-watch && start npm run wp-server"
    

    Every command launched this way starts in its own window.

    0 讨论(0)
  • 2020-11-22 06:17

    In my case I have two projects, one was UI and the other was API, and both have their own script in their respective package.json files.

    So, here is what I did.

    npm run --prefix react start&  npm run --prefix express start&
    
    0 讨论(0)
  • 2020-11-22 06:18

    This worked for me

    {
    "start-express": "tsc && nodemon dist/server/server.js",
    "start-react": "react-scripts start",
    "start-both": "npm -p -r run start-react && -p -r npm run start-express"
    }
    

    Both client and server are written in typescript.

    The React app is created with create-react-app with the typescript template and is in the default src directory.

    Express is in the server directory and the entry file is server.js

    typescript code and transpiled into js and is put in the dist directory .

    checkout my project for more info: https://github.com/nickjohngray/staticbackeditor

    UPDATE: calling npm run dev, to start things off

    {"server": "tsc-watch --onSuccess \"node ./dist/server/index.js\"",
    "start-server-dev": "npm run build-server-dev && node src/server/index.js",
    "client": "webpack-dev-server --mode development --devtool inline-source-map --hot",
    "dev": "concurrently \"npm run build-server-dev\"  \"npm run server\" \"npm run client\""}
    
    0 讨论(0)
  • 2020-11-22 06:19

    I have a crossplatform solution without any additional modules. I was looking for something like a try catch block I could use both in the cmd.exe and in the bash.

    The solution is command1 || command2 which seems to work in both enviroments same. So the solution for the OP is:

    "scripts": {
      "start-watch": "nodemon run-babel index.js",
      "wp-server": "webpack-dev-server",
      // first command is for the cmd.exe, second one is for the bash
      "dev": "(start npm run start-watch && start npm run wp-server) || (npm run start-watch & npm run wp-server)",
      "start": "npm run dev"
    }
    

    Then simple npm start (and npm run dev) will work on all platforms!

    0 讨论(0)
提交回复
热议问题