node.js 中的按键代码错误

Error with keypress code in node.js

我正在试用 node.js 按键模块来监听按键事件。 https://www.npmjs.com/package/keypress

我试用了示例。

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();

我希望示例代码能够正常运行。但是,我收到一个错误

process.stdin.setRawMode(true);
              ^

TypeError: process.stdin.setRawMode is not a function

如何解决这个错误?

代码没有问题。也许您正在尝试 运行 IDE 中的代码?尝试从命令行 运行ning 它;

$ node your_script.name.js