How can I run multiple npm scripts in parallel?

后端 未结 22 2167
灰色年华
灰色年华 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: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");
    })();
    

提交回复
热议问题