REPL
Learn about the REPL module in Node.js.
We'll cover the following
Read Evaluate Print Loop (REPL)
As the heading suggests, REPL does exactly what it stands for. You may already have used a REPL environment before but might not have known about it. Terminals, and even the console on Google Chrome, are REPL environments. A REPL environment can be a quick and easy way to test some code. Think of it as a rough paper to run some code quickly. The REPL reads the code, evaluates it, and prints the output in the next line. This happens in a loop until the REPL exits. This allows us to experiment with the code without having to compile it. The Node.js REPL can be accessed from the command-line, or we can customize and create our own REPL environment using the repl
module.
Commands and special keys
The _
variable
The default evaluator will assign the result of the most recently evaluated expression to the special variable, _
(underscore). However, if we explicitly set _
to a value, it will disable this behavior. Similarly, we can use _error
to access the last error.
Commands
Commands | Explanation |
---|---|
.break |
Breaks from a multiline function |
.clear |
Resets the REPL context and breaks from a multiline function |
.editor |
Enters edit mode |
.exit |
Exits the current session |
.help |
Lists down all commands |
.load |
Loads a file to the current session |
.save |
Saves the current session to a file |
Keys
Keys | Explanation |
---|---|
Ctrl + C |
Press once for a .break or twice for .exit |
Ctrl + D |
Shortcut for .exit |
TAB |
Pressing twice on an empty line prints global and local variables; pressing while entering input will result in an autocomplete or relevant autocomplete options |
Now that we know about its commands, let’s give it a go!
The simple way
The easiest way to access a REPL environment for Node.js is to type node
on the terminal, given Node.js has been installed. You don’t have to worry about that here. Just write node
and give it a go.
Using REPLServer
We can customize our REPL environment by using the REPLServer
class. Importing the repl
module exports repl.REPLServer
. We mentioned context while explaining the clear
method. The context
object is associated with the REPLServer
class and can be used to expose a variable to the REPL. Let’s use it with an example below.
const repl = require('repl'); const msg = 'This is our message'; repl.start('---> ').context.textMessage = msg;
Let’s see what is happening in the code above.
- We import the
repl
module in line 1. msg
is set as a string, This is our message, in line 2.- We start the environment using the
.start
method. Whatever string we pass to this method will be the input prompt to display. Try setting it blank and see what happens. - We set up the
context
object with a new property,textMessage
, and set that equal to themsg
we defined above. We can see our string by typingtextMessage
in the REPL environment. - You can also start the environment without setting the
textMessage
by using just thestart()
method.
Get hands-on with 1300+ tech skills courses.