A Quick Guide to Node.js REPL Commands

A Quick Guide to Node.js REPL Commands

You can run instructions one at a time in the interactive shell that comes with the Node.js runtime. The REPL principle—an acronym for READ, EVALUATE, PRINT, and LOOP—is the foundation of the Node.js interactive shell.

The interactive REPL terminal for Node.js is comparable to a Linux bash terminal, Powershell, or Command Prompt terminal. It carries out the following duties:

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.

We have created a user-friendly online Node.js REPL environment so you may practice Node.js syntax and make learning easier. Go to Node.js Terminal to start the Node.js REPL Terminal.

Enter node in the command terminal (without the javascript file name as done previously) to launch the Node.js REPL on your PC. You’ll see the Node.js prompt >.

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=00>do{... x++;... console.log("x: "+x);...}...while(x<5);x:1x:2x:3x:4x:5undefined>

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=10undefined>var y=20undefined> x+y
30>var z= _
undefined> z
30>

Dot commands

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

Here’s a well-structured table for better readability:

Sr. NoDot CommandDescription
1.helpShows the dot commands help.
2.editorEnables editor mode to write multiline JavaScript code. Press Ctrl+D to execute.
3.breakAborts further input when entering a multi-line expression (same as pressing Ctrl+C).
4.clearResets the REPL context to an empty object and clears any ongoing multi-line expression.
5.loadLoads a JavaScript file relative to the current working directory.
6.saveSaves all entered commands in the REPL session to a specified file.
7.exitExits the REPL session (same as pressing Ctrl+C twice).
8Up/Down KeysNavigates through command history to modify previous commands.
9Tab KeyLists available commands and autocompletes inputs.

This format enhances readability and clarity. Let me know if you need any further refinements! 🚀

It might be helpful:

What’s New in Node.js 21/22? 

Best Practices for API Design in Full-Stack Development

What is REST API with an example?

admin
admin
https://www.thefullstack.co.in

Leave a Reply