Introduction to REPL
Node.js’s REPL is an interactive interface that provides a platform for executing JavaScript commands.
Although REPL is less known to most developers out there, it is actually very useful.
The repl
module is available as a standalone program or can be included in other applications. In this post, I’ll cover how we can directly interact with repl
through the command line interface.
REPL stands for Read-Eval-Print-Loop. This pretty much describes what it does: It reads (receives) the user input, evaluates it according to some user-defined function and prints (displays) the result. We can continue doing this until we exit REPL.
Why is it useful?
What does this all mean?
REPL provides a platform for quick evaluation of JavaScript without opening a new file in the editor, typing there what I want to see and running the file from the terminal window.
For example, I recently stumbled upon the following little quiz on Twitter:
true + false * true + '1'
Instead of opening the editor, creating a new file, saving and running it in the terminal, we can use REPL to quickly get the answer (see below).
REPL contains all JavaScript and Node.js globals including for example Array
and String
methods, as well as, say, the http
module in Node.
How to start REPL
The obvious pre-requisite for REPL is that Node.js has to be installed.
REPL is very easy to start, all we have to do just type node
in the terminal:
$ node
>
The >
shows that REPL is ready to accept our input.
List all globals and locals
Let’s start by listing all global variables and methods we can use.
Press TAB and all available global and local variables will be displayed. Of course, there will initially be no locals, because each time REPL starts, it creates a new context
object, which is empty by default. Once we start defining variables, they will become part of context
and will be added to the list of variables.
So TAB displays a long list of available modules, methods and variables. Let’s now declare and use our own variables.
Define variables
We can use REPL in the same way as we would use JavaScript in the editor.
Let’s quickly declare a variable a
and press Enter:
> const a = 5
undefined
>
As it can be seen, there’s no difference in declaring a
in the editor or doing so in REPL.
Under our input is the return value of the expression we put in, which is undefined
in this case.
If we now type a
, we’ll get the value we have given it:
> a
5
>
Easy.
Use methods
Let’s now investigate an Array
method and how we can use it in REPL. Of course, all other methods can be used in a similar way, so feel free to play around with them.
Assume that we are given a task where each element in an array of numbers should be increased by 6, and the result needs to be returned in another array.
This can be very easily achieved by using the map
method:
> [1,2,3].map(x => x + 6)
[ 7, 8, 9 ]
>
map
returns a new Array
instance, so the return value here will not be undefined
but the new array. REPL displays it nicely formatted, with spaces between each element.
Here we didn’t save the new, returned array in any variable. REPL does that for us though and it stores [ 7, 8, 9 ]
in the _
(underscore) variable:
> _
[ 7, 8, 9 ]
>
The value of _
can be overridden, so when we type in a new value without const
and press Enter, it will also be saved as _
overriding [ 7, 8, 9 ]
:
> 7
7
> _
7
>
Declare functions
Let’s now declare our own function and invoke it.
The function will add 2 to its argument, so let’s call it addTwo
. The function can look like this:
> function addTwo(x) {
... return x + 2;
... }
undefined
>
REPL is smart and when you open a curly brace and press Enter, it will be treated as a multiline entry. We’ll also see undefined
because our function hasn’t been invoked yet.
We can now call addTwo
:
> addTwo(56)
58
>
By now we have some new variables and functions in the local context
as well. Let’s press TAB again to see them.
If you followed along, you should see a
at the bottom of the list as a new variable and addTwo
is at the beginning of the section where the modules and methods are listed.
REPL returns the methods and variables in alphabetical order, so addTwo
will obviously be at the beginning of that list.
We can now solve the above puzzle in REPL:
> true + false * true + '1'
'11'
>
'11'
? true
is converted to 1
and false
is converted to 0
. 1 + 0 * 1 is 1 (order of operations, multiplication comes first), and if we add a number to a string, the number will be converted to a string and they will be concatenated, and that's going to be equal to '11'
.As it can be seen, REPL is very useful when we need to check or evaluate something quickly. It’s a really cool tool and it’s worth giving it a try!
REPL history
Everything we do in REPL is saved into a file called .node_repl_history
. The file containing the persisted history is located in the home directory and its content can easily be viewed (from another terminal window, not from REPL):
cat .node_repl_history
The saving into history feature can be disabled by setting the NODE_REPL_HISTORY
environment variable to ''
.
We can also control how many lines of history we want REPL to save into the persisted history file by setting the value of the NODE_REPL_HISTORY_SIZE
variable. The default is 1000.
REPL commands
We can save the current REPL session to a file other than the persisted history with the .save
command. For example, if we want to save the above session to a file called excerpt-to-repl.js
, we can do the following:
> .save ./excerpt-to-repl.js
Session saved to: ./excerpt-to-repl.js
>
The location of the file will be relative to the directory from which REPL was initiated. For example, if you start REPL from the Projects
folder, .save ./excerpt-to-repl.js
will create and save the file in Projects
, too.
Say we start a new REPL session later. The previously saved session can be loaded into the current REPL session with the .load
command, if we need it:
> .load ./excerpt-to-repl.js
// ... saved session history is displayed here
>
This way all the variables and methods will be available in the new session as if they were declared there.
After we had enough of REPL, we can exit it by typing .exit
(watch the dot) or by pressing Ctrl + D. The .exit
command will take us back to the directory from which REPL was initiated.
REPL has more commands and keyboard shortcuts, and it’s worth having a look at the documentation.
Conclusion
REPL is a less-known but powerful feature of Node.js. It provides us with all global variables and methods available in JavaScript and Node.js.
With REPL we can quickly check something we are not sure about, create functions to evaluate some logic or quickly solve online interview questions. :)
Thanks for reading and see you next time.