How to run a npm run script programatically



  • I have defined a script in my package.json "start" which starts node server.
    I am trying to run execute command "npm run start" in in my code for example:
    exec("npm run start", (error, stdout, stderr) => {
    if (error) {
    console.log(error: ${error.message});
    return;
    }
    if (stderr) {
    console.log(stderr: ${stderr});
    return;
    }
    console.log(stdout: ${stdout});
    });

    But I want to run another script once "npm run start" is executed, example:
    exec("npm run start", (error, stdout, stderr) => {
    if (error) {
    console.log(error: ${error.message});
    return;
    }
    if (stderr) {
    console.log(stderr: ${stderr});
    return;
    }
    console.log(stdout: ${stdout});

    exec("npm run anotherScript", (error, stdout, stderr) => {
    if (error) {
    console.log(error: ${error.message});
    return;
    }
    if (stderr) {
    console.log(stderr: ${stderr});
    return;
    }
    console.log(stdout: ${stdout});
    });
    });

    The issue is it never runs "npm run anotherScript" because promise is still in pending state when I execute "npm run start".

    Can someone please help me to resolve this issue. What could be the best way to run it.


  • Global Moderator

    This isn't a forum for node.js help. It's the community forum for NodeBB, a node.js based forum software.



Suggested Topics

  • 22
  • 3
  • 3
  • 1
  • 4
| |