What is Node.js REPL and its Usage?

Poorshad Shaddel
Webtips
Published in
4 min readJun 26, 2020
What is Node.js REPL and its Usage?
Photo by Pankaj Patel on Unsplash

REPL stands for Read Eval Print Loop and it represents a computer environment like a Windows console or Unix/Linux shell where a command is entered and the system responds with an output in an interactive mode. Node.js or Node comes bundled with a REPL environment. It performs the following tasks(Reference)

What are the usages of REPL?

  • Debug
  • execute code

How can I start working with REPL?

All you need is node on your system. (Installation Guide)

Now if run node command you can get into REPL:

You can run javascript codes in REPL

What does REPL stand for?

  • Read
  • Eval
  • Print
  • Loop

When we write a command and press enter REPL reads our command, execute it and print the result and again it is waiting for another command(this is why it has a loop in its definition).

For example, we want to test a condition like '1' == 1 . We write this command and after execution and print CLI is ready for the next command:

Auto-Completion Option

In REPL you can complete your commands using Tab. for example if we want to see functions and modules inside the global object we can write global. and after that, we can see all global objects using the Tab key twice.

NodeJS REPL Auto-Completion Option

Previous Commands Option

In this command line, you can access your previous commands using up and down arrows. Also, we can access to previous evaluated value using _ :

Node REPL special commands

Special command begin with . and we can see a list of them using the Tab key and you can see a description of these commands using .help

Node REPL special commands
Node Special Commands

.editor mode you can write a multiple-line code and you can finish your code using ctrl + D :

NodeJS REPL editor mode
.editor mode

You may notice that we after calling sayHello function I received an undefined and the reason is that I didn’t return anything from this function and REPL logs return value by default. Also when you use some commands without any return value it logs an undefined for example assigning value to a variable doesn’t return anything:

.exit Does what it says!

.load you can load an external javascript file into your REPL. example:

At first, I needed to get out of REPL to create a file. I used .exit special command and after that, I created a simple js file with only one line of code: const Pi=3.14 then in REPL, I used .load command to import this file into my REPL session and after that, I have access to this file’s variables and functions as you can see.

.save help us saving our session and it’s history into a file for future use. an easy example:

I have assigned two variables and I saved them into a file named mySession and after I loaded it using .load command.

.break and .break can get you out of a copy-paste multi-line session.

Create Your Custom REPL

We can create custom REPL using built-in repl module simply like this:
First create a js file like: myREPL.js

Creating custom REPL

If you are using VSCODE you can access to all REPL options by using ctrl + space . Now if we run this file with node: node myREPL.js we can run commands in our custom REPL:

As you can see there is no undefined cause I passed an option on line four of myREPL.js

--

--

Poorshad Shaddel
Webtips

Full Stack Developer with more than 6 years of expericence. I spend partial part of the day working with Node.js and React.