How to write multiple lines of code in Node REPL

node.jsCommand PromptRead Eval-Print-LoopNode Repl

node.js Problem Overview


I would like to evaluate

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

as a block, instead of evaluating line by line

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

Is there a simple way to move the prompt to the next line?

node.js Solutions


Solution 1 - node.js

Node v6.4 has an editor mode. At the repl prompt type .editor and you can input multiple lines.

example

$ 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.

Here are the docs on all the special repl commands https://nodejs.org/api/repl.html#repl_commands_and_special_keys

Solution 2 - node.js

You can use if(1){ to start a block that will not finish until you enter }. It will print the value of the last line of the block.

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

In multiline mode you miss out on a lot of REPL niceties such as autocompletion and immediate notification of syntax errors. If you get stuck in multiline mode due to some syntax error within the block, use ^C to return to the normal prompt.

Solution 3 - node.js

jhnstn's solution is perfect, but in case you are looking for other alternatives, you can put the code inside a multiline string and then eval it like so:

> let myLongCode = `
... let a = 1;
... let b = 2;
... console.log(a + b);
... `;
> eval(myLongCode)
> 3

Of course this is a hack ;)

Solution 4 - node.js

Node.js REPL supports blocks and is able to return the last expression from a block, so do some other console implementations (Chrome devtools console).

This may result in syntax error, this is a breaking change in Node 10.9.0. { could be a object literal, a block cannot be unambiguously evaluated as a block:

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

While this can be unambiguously evaluated as a block and will return undefined:

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

Since the last expression from a block is logged, console.log isn't needed here:

;{
var foo = "foo";
foo;
}

Notice that this is block scope, so let, const and class won't leak to REPL scope, this behaviour can be desirable or not.

Solution 5 - node.js

Maybe I didn't understand the question well, but if you want to write multiline command in the console of the repl, you can use shift+enter to move to the next lines.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaksanobleView Question on Stackoverflow
Solution 1 - node.jsjhnstnView Answer on Stackoverflow
Solution 2 - node.jsPhssthpokView Answer on Stackoverflow
Solution 3 - node.jsbenjaminzView Answer on Stackoverflow
Solution 4 - node.jsEstus FlaskView Answer on Stackoverflow
Solution 5 - node.jsMihai SabadacView Answer on Stackoverflow