How can I run multiple npm scripts in parallel?

后端 未结 22 2110
灰色年华
灰色年华 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:08

    If you're using an UNIX-like environment, just use & as the separator:

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

    Otherwise if you're interested on a cross-platform solution, you could use npm-run-all module:

    "dev": "npm-run-all --parallel start-watch wp-server"
    
    0 讨论(0)
  • 2020-11-22 06:11

    My solution is similar to Piittis', though I had some problems using Windows. So I had to validate for win32.

    const { spawn } = require("child_process");
    
    function logData(data) {
        console.info(`stdout: ${data}`);
    }
    
    function runProcess(target) {
        let command = "npm";
        if (process.platform === "win32") {
            command = "npm.cmd"; // I shit you not
        }
        const myProcess = spawn(command, ["run", target]); // npm run server
    
        myProcess.stdout.on("data", logData);
        myProcess.stderr.on("data", logData);
    }
    
    (() => {
        runProcess("server"); // package json script
        runProcess("client");
    })();
    
    0 讨论(0)
  • 2020-11-22 06:11

    Just add this npm script to the package.json file in the root folder.

    {
      ...
      "scripts": {
        ...
        "start": "react-scripts start", // or whatever else depends on your project
        "dev": "(cd server && npm run start) & (cd ../client && npm run start)"
      }
    }
    
    0 讨论(0)
  • 2020-11-22 06:12

    A better solution is to use &

    "dev": "npm run start-watch & npm run wp-server"
    
    0 讨论(0)
  • 2020-11-22 06:12
    npm install npm-run-all --save-dev
    

    package.json:

    "scripts": {
      "start-watch": "...",
      "wp-server": "...",
      "dev": "npm-run-all --parallel start-watch wp-server"
    }
    

    More info: https://github.com/mysticatea/npm-run-all/blob/master/docs/npm-run-all.md

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

    You can use one & for parallel run script

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

    Reference link

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