Gary Sieling

Node: execSync example

In Node.js, you can run shell commands if you import child process:

const child_process = require('child_process');

You can run these inline, and using the same console output as your script, if you send “stdio” as an argument to execSync.

const cmd = 'd:/Software/ffmpeg-20160619-5f5a97d-win32-static/bin/ffmpeg.exe -i ' +  id + '.en.vtt ' + id + '.srt';

child_process.execSync(
  cmd, 
  {
    cwd: 'd:/projects/youtube-scraper/videos',
    stdio: [0, 1, 2]
  });

If you don’t change stdio, you will get a buffer back, which contains the output of the script.

Changing the working directory is also handy. I found experimentally that if you do this on Windows, you need to use Windows style paths (note: if you are using the new bash for Windows, you instead need to treat this as if it were Ubuntu).

I also found experimentally that some shell scripts will pop up Windows, and run asynchronously. I was able to fix this by inlining the shell script into separate Node commands.

Exit mobile version