Search code examples
node.jsstdinkeypress

Error with keypress code in node.js


I am trying out the node.js keypress module to listen for keypress events. https://www.npmjs.com/package/keypress

I tried out the sample example.

var keypress = require('keypress');

// make `process.stdin` begin emitting "keypress" events 
keypress(process.stdin);

// listen for the "keypress" event 
process.stdin.on('keypress', function (ch, key) {
  console.log('got "keypress"', key);
  if (key && key.ctrl && key.name == 'c') {
    process.stdin.pause();
  }
});

process.stdin.setRawMode(true);
process.stdin.resume();

I would expect the sample code to work without error. However, I received an error

process.stdin.setRawMode(true);
              ^

TypeError: process.stdin.setRawMode is not a function

How to fix this error?


Solution

  • There is nothing wrong with the code. Perhaps you are trying to run the code in an IDE? Try running it from command-line;

    $ node your_script.name.js