如何在Node REPL中编写多行代码

我想评估

var foo = "foo"; console.log(foo); 

作为一个块,而不是逐行评估

 var foo = "foo"; undefined console.log(foo); foo undefined 

有一种简单的方法将提示移到下一行吗?

Node v6.4有一个editor模式。 在repl提示符下键入.editor ,您可以input多行。

 $ node > .editor // Entering editor mode (^D to finish, ^C to cancel) const fn = there => `why hello ${there}`; fn('multiline'); // hit ^D 'why hello multiline' > // 'block' gets evaluated and back in single line mode. 

以下是所有特殊repl命令的文档https://nodejs.org/api/repl.html#repl_commands_and_special_keys

你可以使用if(1){来启动一个块,直到你input完成。 它将打印块的最后一行的值。

 > { ... var foo = "foo"; ... console.log(foo); ... } foo undefined 

在多行模式下,你错过了很多REPL的细节,比如自动完成和语法错误的即时通知。 如果由于块内的某些语法错误而陷入多行模式,请使用^C返回正常提示。