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';
    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.

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *