What is Node.js REPL and its Usage?
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:
What does REPL stand for?
- Read
- Eval
- 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.
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
.editor
mode you can write a multiple-line code and you can finish your code using ctrl + D
:
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
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