Node.js – REPL Terminal


Node.js – REPL Terminal


”;


The Node.js runtime has a built-in interactive shell, in which you can execute instructions one at a time. The Node.js interactive shell works on the principle of REPL, which is an acronym for READ, EVALUATE, PRINT and LOOP.

The Node.js interactive REPL terminal is like the Powershell or Command prompt terminal, or a bash terminal in Linux. It performs the following tasks −

Read − Reads user”s input, parses the input into JavaScript data-structure, and stores in memory.

Eval − Takes and evaluates the data structure.

Print − Prints the result.

Loop − The terminal is ready to receive next input from the user.

To simplify your learning, we have set up an easy to use Node.js REPL environment online, where you can practice Node.js syntax − To launch Node.js REPL Terminal, visit Node.Js Terminal

To start the Node.js REPL on your computer, simply enter node in the command terminal (without the javascript file name as done before). The Node.js prompt > will appear.

D:nodejs>node
Welcome to Node.js v20.9.0.
Type ".help" for more information.
>

The REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.

You can test any Node.js/JavaScript expression by entering in front of the > prompt. For example −

> 10+20
30
> "Hello"+"World"
''HelloWorld''
> a=10
10
> b=20
20
> a+b
30
> Math.random()
0.5423940959293392
>

You can see that the instruction is read, evaluated, its result displayed and the terminal is ready to receive next instruction. To start the REPL, press ctrl+c twice, or ctrl+D, or enter .exit in front of > symbol.

Multiline Expression

Node REPL supports multiline expression similar to JavaScript. Let”s check the following do-while loop in action −

> x=0
0
> do {
... x++;
... console.log("x: "+x);
... }
... while (x<5);
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>

The three dots … comes automatically when you press Enter after the opening bracket. Node automatically checks the continuity of expressions.

Underscore Variable

You can use underscore (_) to get the last result −

> var x=10
undefined
> var y=20
undefined
> x+y
30
> var z= _
undefined
> z
30
>

Dot commands

The REPL has some special commands, all starting with a dot .. They are

Sr.No Dot Commands & Description
1

.help

shows the dot commands help

2

.editor

enables editor mode, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the code you wrote.

3

.break

when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-C.

4

.clear

resets the REPL context to an empty object and clears any multi-line expression currently being input.

5

.load

loads a JavaScript file, relative to the current working directory

6

.save

saves all you entered in the REPL session to a file (specify the filename)

7

.exit

exits the repl (same as pressing ctrl-C two times)

8

Up/Down Keys

see command history and modify previous commands.

9

tab Keys

list of current commands.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *