1

I want to take the output of a c++ program and input it into the stdin of a javascript file. However I have been unable to push anything into the stdin using the method...

node example.js < test.txt

because I get the following errors.

example.js:35

stdin.setRawMode( true );

TypeError: undefined is not a function

at Object. (example.js:35:7)

at Module._compile (module.js:460:26)

at Object.Module._extensions..js (module.js:478:10)

at Module.load (module.js:355:32)

at Function.Module._load (module.js:310:12)

at Function.Module.runMain (module.js:501:10)

at startup (node.js:129:16)

at node.js:814:3

The offending code appears to be as below. It works fine during normal input, however crashes in the above scenario.

var stdin = process.stdin;
stdin.setRawMode( true );
stdin.resume();
stdin.setEncoding( 'utf8' );
stdin.on( 'data', function( key ){
//do stuff based upon input

Has anyone encountered this, or any ideas about how to fix it? Or another way of going about this problem?

3
  • What platform/os are you on? Commented Mar 10, 2015 at 2:34
  • osx 10.10 although I also want it to work on raspberry PI Commented Mar 10, 2015 at 2:50
  • Actually, scratch that. I'm seeing the same behavior when redirecting stdin. Investigating further. Commented Mar 10, 2015 at 2:54

2 Answers 2

4

When running your program with redirected stdin, you're connected to a ReadStream, not a TTY, so TTY.setRawMode() isn't supported.

setRawMode() is used to set a tty stream so that it does not process its data in any way, such as providing special handling of line-feeds. Such processed data is referred to as being "cooked".

Standard node ReadStreams are, by definition, already "raw" in that there is no special processing of the data.

So, refactor your code without the call to setRawMode() and it should work fine.

Sign up to request clarification or add additional context in comments.

Comments

0

This is how I process data from STDIN in Node:

function readStream(stream, callback) {
  var data = '';
  stream.setEncoding('utf-8');
  stream.on('data', onData);
  stream.on('end', onEnd);

  function onData(chunk) {
    data += chunk;
  }

  function onEnd() {
    stream.removeListener('end', onEnd);
    stream.removeListener('data', onData);
    callback(data);
  }
}

The error message in your questions (the "undefined is not a function") error means you were trying to call a method on stdin that doesn't exist. I couldn't find a mention of setRawMode in a cursory scan of the docs. Perhaps you are using an outdated API? I recall having to unpause STDIN (old-style streams) is deprecated.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.