Search code examples
node.jsangularnodes

How to generate angular application using child_processes.spawn() method in a specific directory?


I have recently started to study about node and I wanted the node server should use:

ng new my-app

To create application without manually typing in the terminal. I found that we could run command using child_processes.spawn() or child_processes.exec() in node. I cannot understand why am i not able to do so with the below code?

spawn("ng",[join("ng new ", folderName," --directory ", workspaceName)]);

I am new to this topic so I would require your help to understand this.


Solution

  • Try This

    var spawn = require('child_process').spawn;
    
    var child = spawn('npm install -g @angular/cli && cd your directory && ng new my-dream-app', {
      shell: true
    });
    child.stderr.on('data', function (data) {
      console.error("STDERR:", data.toString());
    });
    child.stdout.on('data', function (data) {
      console.log("STDOUT:", data.toString());
    });
    child.on('exit', function (exitCode) {
      console.log("Child exited with code: " + exitCode);
    });