Skip to main content
added 302 characters in body
Source Link
Kir Chou
  • 3.1k
  • 1
  • 43
  • 51

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'}); It's correct?

The code is fine if your parameters of spawn are controllable. But generally, I suggest use pure spawn without using shell. The risk will reduce without touching shell directly.


Since you need the stream return from spawn. I have checked other solution here. Without shell: true, You can try this.use the code:

const {
  spawn
} = require('child_process');

let projectPath = ''//the path of your project
let info = spawn('npm', ['-v'], { cwd: projectPath });

let result = '';
info.stdout.on('data', function(data) {  
  result += data.toString();
  console.log(result);
}

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

Since you need the stream return from spawn. I have checked other solution here. You can try this.

const {
  spawn
} = require('child_process');

let projectPath = ''//the path of your project
let info = spawn('npm', ['-v'], { cwd: projectPath });

let result = '';
info.stdout.on('data', function(data) {  
  result += data.toString();
  console.log(result);
}

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'}); It's correct?

The code is fine if your parameters of spawn are controllable. But generally, I suggest use pure spawn without using shell. The risk will reduce without touching shell directly.


Since you need the stream return from spawn. I have checked other solution here. Without shell: true, You can use the code:

const {
  spawn
} = require('child_process');

let projectPath = ''//the path of your project
let info = spawn('npm', ['-v'], { cwd: projectPath });

let result = '';
info.stdout.on('data', function(data) {  
  result += data.toString();
  console.log(result);
}
added 171 characters in body
Source Link
Kir Chou
  • 3.1k
  • 1
  • 43
  • 51

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

Instead of usingSince you need the stream return from spawn in your case,. I suggest to usehave checked other solution exechere. With the usage below, you also able to get the stdout in the closureYou can try this.

const {
  execspawn
} = require('child_process');

let cmdprojectPath = 'npm''//the -v';
path of your project
execlet info = spawn(cmd'npm', function(error['-v'], { cwd: projectPath });

let result = '';
info.stdout.on('data', stderrfunction(data) {  
  result += data.toString();
  console.log(stdoutresult);
});

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

Instead of using spawn in your case, I suggest to use exec. With the usage below, you also able to get the stdout in the closure.

const {
  exec
} = require('child_process');

let cmd = 'npm -v';

exec(cmd, function(error, stdout, stderr) {
  console.log(stdout);
});

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

Since you need the stream return from spawn. I have checked other solution here. You can try this.

const {
  spawn
} = require('child_process');

let projectPath = ''//the path of your project
let info = spawn('npm', ['-v'], { cwd: projectPath });

let result = '';
info.stdout.on('data', function(data) {  
  result += data.toString();
  console.log(result);
}
Source Link
Kir Chou
  • 3.1k
  • 1
  • 43
  • 51

While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.

Instead of using spawn in your case, I suggest to use exec. With the usage below, you also able to get the stdout in the closure.

const {
  exec
} = require('child_process');

let cmd = 'npm -v';

exec(cmd, function(error, stdout, stderr) {
  console.log(stdout);
});