How to write multiple lines of code in Node REPL
Asked Answered
M

6

72

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?

Marcmarcano answered 23/3, 2015 at 5:39 Comment(3)
no. repl automativally starts "multiline mode" if there is syntax error ( and exits it when expression is valid ). Why line by line is not good enough for you? You can still paste multiline input and all lines from pasted text will be evaluatedKerry
I find the line by line evaluation distracting when I'm writing a small block of code in the REPL.Marcmarcano
Alternatively, you could use Node debugging features in Chrome. I guess you can enter multiple lines in the developer console, which would serve as a Node repl.Tree
N
133

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

Nyasaland answered 24/8, 2016 at 21:49 Comment(1)
Extra credit for the link with the remaining special commands. Thanks!Randers
L
27

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.

Libertine answered 23/3, 2015 at 5:46 Comment(4)
don't work for me ( I assume "{" is interpreted as beginning of object ). Works if first line is if(1) {Kerry
Good catch. I have no idea why it's so impossible to recover from other starting points such as (Libertine
A note of caution: if you copy/paste a block like the above which includes a tab character, the tab character will trigger node's autocompletion (probably not what you want), and most likely will cause the node to quit.Finegan
@AndreySidorov The braces-only (i.e. no "if(1)") version works for me, with node version 6.9.1.Termitarium
R
4

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

Rutledge answered 21/3, 2018 at 21:36 Comment(0)
S
1

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.

Strap answered 26/1, 2019 at 22:8 Comment(1)
I was referring to the Node console and not the browser console.Marcmarcano
F
0

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.

Foreordination answered 25/10, 2018 at 10:22 Comment(0)
G
0

You can also use some applications like runjs if you don't insist on using the node repl and just want to try something in node.js and javascript in general

https://runjs.app/

Gerome answered 26/3, 2023 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.