Node.js - REPL Terminal



The Node.js provides a REPL terminal, that allows to execute the JavaScript code interactively. It is an essential tool for testing, debugging and experimenting with JavaScript and Node.js features. The REPL is used to check the JavaScript functionality without setting up an entire development environment.

What is REPL

It is an interactive programming environment that allows users to run the JavaScript code directly in the terminal, It stands for:

  • Read − It reads the user input and analyses it into the JavaScript expressions.
  • Eval − It evaluates the analyzed JavaScript expressions.
  • Print − It prints the evaluation result.
  • Loop − Loops back to read the next input.
The REPL environment is automatically available, when we install the Node.js making it convenient for quick testing and debugging.

Starting the REPL

The REPL can be accessed directly from the command line. This makes it easy to write and test the JavaScript code without creating the separate script files. For launching the REPL terminal, open the command prompt or terminal and type:

node

This result in the REPL prompt:

Welcome to Node.js v22.13.1.
Type ".help" for more information.
>

Using REPL

The REPL terminal provides a environment, where users can execute the JavaScript expressions, perform calculations, declare variables and define the functions interactively

Running JavaScript Expressions

One of the key features of REPL is it ability to execute the JavaScript expressions on the go. It allows users to quickly test the JavaScript syntax, mathematical calculations, and function outputs without the need of script file.

Let's look at the simple example of executing the JavaScript expressions in REPL.

> 2 + 3
5
> 11 - 22
-11
> console.log("Welcome to TP.!")
Welcome to TP.!
undefined

The REPL evaluates the expressions and prints the result immediately, making it as a good tool for rapid experimentation.

Variable Declarations

In REPL, we can declare variables dynamically using the var, let or const. It allows users to experiment with different variable scopes and behaviour without the need of separate script file. Variables declared in the REPL continue throughout the session, making it useful for calculations and debugging.

> let a = 1121;
undefined
> a * 2
2242

Multiline Code Execution

It supports executing the multi-line JavaScript code, which is particularly useful for defining functions, loops and complex statements that requires multi lines.

When we try to enter an incomplete expression, REPL automatically enters multiline mode, allowing to finish the incomplete code. It allows the users to test more structured and logical code within the REPL session.

> function x(a){
... return "Welcome To, " +a;
... }
undefined
> x("TutorialsPoint")
'Welcome To, TutorialsPoint'

Using Underscore

The REPL automatically stores the result of the last evaluated expression in a special variable underscore(_). It allows the users to reuse the last output without retyping the entire expression. Let's consider the simple example for understanding better

> 112 + 234
346
> _ / 2
173

Here, the _ variable holds the value 346, and then it is divided by 2 to get 173.

Working with Global Objects

The Node.js REPL provides the direct access to the global objects, making it easy for the users to interact with system-level functionalities.

Global objects in Node.js such as process, console and Buffer can be accessed without requiring the additional imports. These objects provides the essential capabilities like handling system information, managing buffers.

Process

For example, we are going to consider the process object which provides the information about the current Node.js runtime.

> console.log(process.version)
v22.13.1
Buffer

Now, we are going to use the Buffer object, which allows to manipulate the binary data.

> Buffer.from('TutorialsPoint')
<Buffer 54 75 74 6f 72 69 61 6c 73 50 6f 69 6e 74>

Handling Errors

The REPL provides the meaningful error message to help the users to debug the issues. It is particularly useful for testing the small code snippets before dumping them into a larger application. Common errors like syntax errors, type errors and reference errors can be handled by detailed feedback.

> let y = 123
undefined
> y()
Uncaught TypeError: y is not a function

REPL Commands

The Node.js REPL includes the several built-in commands for better usability, some of them are listed below:

S.No Dot Commands & Description
1

.exit

It exits the REPL session.

2

.clear

It clears the REPL environment.

3

.save

It saves the REPL session to a file.

4

.load

It loads a file into the REPL session.

5

.editor

It enters the multiline editor mode.

Advertisements