Solution:
spawn gnuplot directly var term = require('child_process').spawn('gnuplot');
instead of the bash.
remove term.stdin.write('gnuplot -p\n');
(-p flag makes gnuplot windows persistent, but there's no need for this because in this case plots are printed to png images)
add term.stdin.write('exit\n')
at the end.