# Run Command Not Executing Node

How do I make the following command work in Mathematia?

Import["!\"C:\\Program Files\\nodejs\\node.exe\" -p console.log('test')", "Text"]


I don't get any output as if the command didn't properly execute. If I execute the following my command does execute so it is simply a problem with the execute/displays output. Also Import["!where node", "Text"] works so it is in the path.

Import["!node -h", "Text"]


EDIT: I can't reproduce this in any other application other then Mathematica so it isn't clear to me what the issue is.

• What is that supposed to do? What happens when you type "node -p console.log('test')" into the command line? – Jason B. Nov 16 '15 at 8:04
• Also, is the node command in your PATH? Did you verify that it is still in your PATH when using Mathematica (GetEnvironment)? – Szabolcs Nov 16 '15 at 12:44
• @Szabolcs Yes it is in my command path because node test.js works. I added an exact path so that is no longer a question. I feel like there is enough information to reproduce the bug. – William Nov 16 '15 at 16:49
• @JasonB Just 'test' as output. There are a lot of commands that don't work ffmpeg -help doesn't work also I feel like there is enough information to reproduce the bug and therefore the question shouldn't have been closed as is. – William Nov 16 '15 at 16:51
• @WReach You deserve a 100 point bounty for discovering this. Please post that as a response if you don't mind(although it is a node fix and not a mathematica). Again you don't realize how much I appreciate this. – William Nov 16 '15 at 20:26

This is a known problem with nodejs on Windows. The stdout stream is an asynchronous stream and is not always being flushed before the process exits. A work-around is to perform an explicitly synchronous write to stdout (i.e. file descriptor #1):

Import["!node -e require('fs').writeSync(1,'test')", "Text"]

(* test *)


The bug report referenced above claims that the difficulty is fixed in nodejs version 0.12 and above (although my own experiments are mixed).

In nodejs 4.2.2 for Windows, and despite documentation to the contrary, it appears that console.log sends its output to stderr instead of stdout. We can redirect such output onto stdout by using 2>&1 on the command line:

Import["!node -e console.log('test') 2>&1", "Text"]

(* test *)


On this version we can also write directly to process.stdout:

Import["!node -e process.stdout.write('test')", "Text"]

(* test *)

• You don't have to answer this but is there a way to reproduce this simply in command prompt or powershell(or python or nodejs even)? I would like to report this to node but I can't seem to reproduce this without mathematica. I'm still wondering if this is a little bit of a Mathematica bug. – William Nov 24 '15 at 3:57
• Following want you said it doesn't seem like the following should work node -e "require('child_process').exec('node -e console.log(\'test\') ',function(e,o,s){console.log(o)})" but it does. – William Nov 24 '15 at 4:32
• I tried a number of experiments using various other applications in place of Mathematica or node, and all of them worked. Only the Mathematica/node combination failed. The fact that the "synch" work-around is effective offers some sort of clue, but I'm stumped as to what it means. I spent a few minutes in PROCMON trying to spot something unusual, but to no avail. Most interestingly, using the command line version of the Mathematica kernel works -- only the notebook interface fails. – WReach Nov 24 '15 at 16:00