Here is a test program called test-program.js where I run an imagemagick command, and try print stdout of the command to stdout of this program, and then save it to a file:
const util = require('util'); const exec = util.promisify(require('child_process').exec); (async function() { const cmd = `convert -background red -fill black -gravity center -size 50x50 label:':)' jpg:-`; const result = await exec(cmd); console.log(result.stdout); })(); I try to run it like this
node test-program.js > test.jpg and the jpg will not open
However, if I run it right on the command line like this:
convert -background red -fill black -gravity center -size 50x50 label:':)' jpg:- > test.jpg here are two versions of the program that do work as expected:
call command with node, but make the command write directly to the file
const util = require('util'); const exec = util.promisify(require('child_process').exec); (async function() { const cmd = `convert -background red -fill black -gravity center -size 50x50 label:':)' test.jpg`; const result = await exec(cmd); console.log(result.stdout); })(); just write 'hello' into the file with a simpler command:
const util = require('util'); const exec = util.promisify(require('child_process').exec); (async function() { const cmd = `echo hello`; const result = await exec(cmd); console.log(result.stdout); })(); So I am pretty sure the command is executing as it should, that it is producing the output it should, and that the wrapper usually writes the output to stdout without a problem. (or maybe with an extra line at the end)
I have also tried converting the string to a buffer with no luck.


node, but a quick look at the documentation forexec()leads me to think you may need to a) change the encoding from its default of utf8 to binary since JPEGs are binary, and maybe b) increase the buffer size. I have no idea how to do these things, but maybe someone else can fill in the blanks to get you going... teamwork!exec