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.

  • 3
    $\begingroup$ What is that supposed to do? What happens when you type "node -p console.log('test')" into the command line? $\endgroup$
    – Jason B.
    Nov 16, 2015 at 8:04
  • 2
    $\begingroup$ Also, is the node command in your PATH? Did you verify that it is still in your PATH when using Mathematica (GetEnvironment)? $\endgroup$
    – Szabolcs
    Nov 16, 2015 at 12:44
  • $\begingroup$ @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. $\endgroup$
    – William
    Nov 16, 2015 at 16:49
  • $\begingroup$ @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. $\endgroup$
    – William
    Nov 16, 2015 at 16:51
  • 1
    $\begingroup$ @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. $\endgroup$
    – William
    Nov 16, 2015 at 20:26

1 Answer 1


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 *)
  • $\begingroup$ 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. $\endgroup$
    – William
    Nov 24, 2015 at 3:57
  • $\begingroup$ 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. $\endgroup$
    – William
    Nov 24, 2015 at 4:32
  • 1
    $\begingroup$ 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. $\endgroup$
    – WReach
    Nov 24, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.