Nodejs is open-source javascript technology built on google chrome's v8 engine. It is a run-time environment for server-side applications in javascript.
REPL stands for Read Evaluation Print Loop. It is a computer environment similar to a command prompt that allows the users to interact with the system through commands. It is installed while installing Node js in the system. It allows to execute the code in bite-size chunks, like the use of console in google chrome is the same as the use of REPL. It is used for executing and debugging javascript code.
Terms in REPL
Read
It reads the inputs from the user and parses them into javascript data structure and stores them in the memory.
Evaluation
The parsed javascript data structure is evaluated for the results.
The result is printed after the evaluation.
Loop
It loops the input command until the user presses ctrl+c twice.
Getting started with REPL environment
First, you need to download Nodejs to your system. After installing Nodejs, REPL gets installed in the system. To set up a REPL environment open a command prompt in windows or you can open a terminal in Linux and type node command in the command line, this will start REPL with the '>' symbol.
REPL environment
Introduction to REPL module
Nodejs provides a built-in repl module. This module can be used to create our REPL. It exports repl.REPLServer class which takes the input line by line through stdin and gives the output through stdout. REPL can be implemented with standalone repl through the command line or can be included in other applications.
We can include the repl module using 'require' in the javascript file. Create a repl.js file and write the following statement.
require repl module
This will include the repl module. Now we can perform functionalities using the repl variable.
To start the repl command prompt write the following statement
start command
When we start repl in command prompt by writing node command it executes the above code.
repl.start() starts the repl environment and the prompt is a string that accepts the prompt which shows when repl starts, default is '>'. We can define custom prompts, below is the example.
repl prompt
To run this file open command and type node repl.js
command line
There are special commands that can be executed in the command prompt. We can get all the list of commands with their description writing .help command or type '.' and press double tab to see all the commands.
.help command
Instances of repl.REPLServer uses the evaluation function to evaluate the javascript expression.
We can execute various javascript expressions in REPL.
javascript expression
For multiline command, repl adds '...' dots on its own to the beginning of the next line. Break command can be used to terminate the multiline command.
By hitting the tab twice the shell displays all the methods and attributes of Node js.
Editor mode in repl
editor mode in repl
To execute editor mode press ctrl + d.
To exit repl press ctrl + c twice and to clear the console clear command is used.
While exiting, repl listens to the ' exit' event and executes the given statement.
exit event
This will print the log statement when the .exit command is typed.
exit command
Application of REPL
Nodejs REPL can be used for debugging the code and running javascript applications.
Nodejs is fast and scalable for creating a web application, and it provides a REPL environment for experimenting with code.