REPL module in node.js


Introduction
REPL (Read , Eval , Print , Loop ) is a node.js module which can be used to
  1. READ : Read user's input , parse it and store it into memory.
  2. Eval : Evaluate the data provided by the user.
  3. Print : Print the Result.
  4. Loop : Repeat the process unless user exits it using the command.
It can be used as an Standalone application or can also be included in other applications. We can access it via :
		        
const repl = require('repl');
				
	            

Let's begin with REPL
In order to begin with repl , you need to type the following in your bash :
		        
$ node
                
	            

After running you will observe something like this on your bash :
		        
$ node
>
                
	            

Commands and Special Keys in REPL
This is the list of special commands supported by REPL :
  • .help
  • .break
  • .clear
  • .editor
  • .save
  • .load
  • .exit
.help
It is used to print the following help message on the bash.
		        
C:\Users\acer>node
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
>
                
	            

.break
If you are writing a multi-line expression , then this command will abort the further processing of input. It is similar to pressing ctrl+C key once which will also produce the same result.
		        
C:\Users\acer>node
> function demo(){
... console.log("just an example");
... .break
>
                
	            

.clear
It is used to clear any multi-line expression if being inputted.
		        
C:\Users\acer>node
> function random() {
... console.log("some value");
... .clear
>
                
	            

.editor
It is used to enter the editor mode in which we can write expressions same as we write in any other GUI editor.
		        
C:\Users\acer>node
> .editor
// Entering editor mode (^D to finish, ^C to cancel)
function hello(name){
return ` Hello ${name} !`;
}

> hello(`nodejsera`);
' Hello nodejsera !'
>
                
	            

.save
It is used to save the current REPL session in a file.
		        
C:\Users\acer>node
> function add(a,b) {
... return `addition is ${a+b}`;
... }
undefined
> add(10,20);
'addition is 30'
> .save /add.js
Session saved to:/add.js
>
                
	            

.load
It is used to launch an already saved REPL session.
		        
C:\Users\acer>node
> .load /add.js
> function add(a,b) {
... return `addition is ${a+b}`;
... }
undefined
> add(10,20);
'addition is 30'
>
                
	            

.exit
It is used to stop the I/O stream and exit the REPL mode.
		        
>node
> .load /add.js
> function add(a,b) {
... return `addition is ${a+b}`;
... }
undefined
> add(10,20);
'addition is 30'
>.exit
C:\Users\acer>

                
	            

Summary
  1. Intoduction to repl
  2. Let's begin with repl
  3. Commands and Special Keys in REPL