Table of contents
1.
Introduction
2.
What is Node.js REPL?
3.
Getting Started with REPL
3.1.
Step 1: Install Node.js
3.2.
Step 2: Open REPL
3.3.
Step 3: Execute JavaScript Code
3.4.
Step 4: Exit REPL
4.
Key Features of Node.js REPL
4.1.
1. Immediate Feedback
4.2.
2. Multi-line Code Execution
4.3.
3. Variable and Function Definitions
4.4.
4. Tab Completion
4.5.
5. Saving and Loading Code
5.
Built-in REPL Commands
5.1.
1. .help
5.2.
2. .break
5.3.
3. .clear
5.4.
4. .exit
5.5.
5. .load
5.6.
6. .save
6.
Mastering REPL Shortcuts
6.1.
1. Starting REPL
6.2.
2. Navigating REPL
6.3.
3. Using Underscore (`_`) for Last Result
6.4.
4. Multi-line Code Execution
7.
Frequently Asked Questions
7.1.
What is REPL in Node.js?
7.2.
How do I exit the REPL in Node.js?
7.3.
Can I save my REPL session?
8.
Conclusion
Last Updated: Jan 20, 2025
Medium

How To Use the Node.js REPL

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Node.js REPL (Read-Eval-Print Loop) is a powerful tool that allows developers to run JavaScript code directly in a command-line interface. It is commonly used for testing small pieces of code, debugging, or learning JavaScript concepts. The REPL provides an interactive environment where you can type commands, evaluate them, and see the results instantly.

How To Use the Node.js REPL

In this article, we will discuss REPL in Node.js, how to get started with it, its key features, and the built-in commands it offers.

What is Node.js REPL?

Node.js REPL is an interactive environment that lets developers execute JavaScript code line by line. It is especially useful for:

  • Testing small snippets of code.
     
  • Debugging JavaScript logic.
     
  • Learning JavaScript concepts interactively.
     

When you use the REPL, it works in the following manner:

  1. Read: It reads the user’s input.
     
  2. Eval: It evaluates the inputted code.
     
  3. Print: It prints the result of the evaluation.
     
  4. Loop: It loops back to accept the next input.
     

For example, you can calculate a sum directly in the REPL:

> 5 + 10
15


The REPL will evaluate 5 + 10 and instantly print the result, 15.

Getting Started with REPL

To start using the REPL in Node.js, follow these steps:

Step 1: Install Node.js

Ensure that Node.js is installed on your system. You can check this by running the following command in your terminal:

node -v

If Node.js is installed, this command will return the version number.

Step 2: Open REPL

To access the REPL environment, type node in your terminal and press Enter. You will see the REPL prompt (>), indicating that it’s ready to accept JavaScript code.

$ node
>

Step 3: Execute JavaScript Code

You can now start typing JavaScript expressions or commands. For example:

> console.log('Hello, REPL!');

Hello, REPL!

undefined

Here, the REPL executes console.log() to print the string Hello, REPL!. The undefined output indicates the return value of console.log().

Step 4: Exit REPL

To exit the REPL, type .exit or press Ctrl + C twice.

Key Features of Node.js REPL

1. Immediate Feedback

The REPL evaluates and displays the result of any JavaScript expression you type instantly. This is useful for testing small snippets of code without creating files.

Example:

> 100 * 2
200

2. Multi-line Code Execution

You can enter multi-line code in the REPL by using curly braces {}. This allows you to test more complex logic interactively.

Example:

> {
... let a = 10;
... let b = 20;
... a + b;
... }
30

Here, the REPL evaluates the entire block and returns the result.

3. Variable and Function Definitions

You can define variables and functions in the REPL and use them as needed.

Example:

> let greet = (name) => `Hello, ${name}!`;
undefined
> greet('Alice');

'Hello, Alice!'

4. Tab Completion

REPL offers tab completion, making it easier to explore available methods and properties. For instance, typing con and pressing Tab will suggest console.

5. Saving and Loading Code

The REPL allows you to save your session to a file and reload it later. This is useful for preserving code snippets.

Commands:

> .save filename.js  # Saves the current session to filename.js

> .load filename.js  # Loads the session from filename.js

Built-in REPL Commands

The Node.js REPL provides several built-in commands to enhance usability. These commands begin with a . and are not part of the JavaScript language itself.

1. .help

Displays a list of all REPL commands.

> .help


Output:

.break    Exit from multi-line expressions

.clear    Clear the REPL context

.exit     Exit the REPL

.help     Show REPL commands

.load     Load JavaScript from a file

.save     Save all REPL commands to a file

2. .break

Exits a multi-line expression. If you’re stuck in an incomplete code block, use .break.

Example:

> {
... let x = 10
... .break
>

3. .clear

Clears the current REPL context, removing all variables and functions.

> .clear

This is useful for starting fresh without exiting the REPL.

4. .exit

Exits the REPL environment.

> .exit

5. .load

Loads JavaScript code from a file into the REPL.

> .load example.js

6. .save

Saves the current REPL session to a file.

> .save session.js

Mastering REPL Shortcuts

REPL in Node.js is not just a tool for running code; it’s also packed with shortcuts & features that can make your coding experience faster & more efficient. These shortcuts help you navigate, edit, & execute code without leaving the REPL environment. Let’s understand this one by one.

1. Starting REPL

To start REPL, open your terminal & type `node`. This will launch the REPL environment, & you’ll see a `>` prompt where you can start typing JavaScript code.

$ node
>


Once you’re inside REPL, you can start typing any valid JavaScript code, & it will execute immediately. For example:

> console.log("Hello, REPL!");
Hello, REPL!
undefined


The `undefined` you see is the return value of the `console.log` statement. REPL always shows the result of the last executed expression.

2. Navigating REPL

REPL provides several keyboard shortcuts to help you navigate & edit your code efficiently. Here are some of the most useful ones:

  • Up/Down Arrow Keys: Use these keys to cycle through your command history. This is helpful when you want to reuse or modify a previous command.
     
  • Tab Key: Pressing the Tab key will autocomplete your code. For example, if you type `cons` & press Tab, REPL will automatically complete it to `console`.
     
  • Ctrl + C: This shortcut stops the current command. If you’re stuck in a loop or waiting for input, this will cancel the operation & bring you back to the REPL prompt.
     
  • Ctrl + D: This exits the REPL environment & returns you to the terminal.

3. Using Underscore (`_`) for Last Result

REPL provides a special variable `_` (underscore) that stores the result of the last executed expression. This is helpful when you want to reuse the result of a previous calculation.

> 5 + 5
10
> _ + 10
20


In this example, `_` holds the value `10` from the previous operation, & adding `10` to it gives `20`.

4. Multi-line Code Execution

REPL allows you to write multi-line code, such as functions or loops. To start a multi-line block, simply press Enter after an opening brace `{`. REPL will automatically detect that you’re writing a multi-line statement & will show `...` for each new line.

> function greet(name) {
... console.log(`Hello, ${name}!`);
... }
undefined
> greet("Alice");
Hello, Alice!
undefined


Here, the function `greet` is defined over multiple lines, & then it’s called with the argument `"Alice"`.

Frequently Asked Questions

What is REPL in Node.js?

REPL stands for Read-Eval-Print-Loop. It is an interactive shell in Node.js that allows you to execute JavaScript code directly in the terminal.

How do I exit the REPL in Node.js?

You can exit the REPL by typing .exit or pressing Ctrl + C twice.

Can I save my REPL session?

Yes, you can save your session to a file using the .save command and reload it later using .load.

Conclusion

In this article, we discussed the REPL environment in Node.js, its key features, and built-in commands. The REPL is an excellent tool for developers to quickly test JavaScript code, debug logic, and experiment interactively. 

Live masterclass