codedamn logo
codedamn

What is REPL in Node.js? How To Use & Explanation with an Example?

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
What is REPL in Node.js? How To Use & Explanation with an Example?

What is REPL?

REPL is a command-line tool that takes the input from the user and gives the desired output with the series of events undergoing between the two actions, one being the input and the other being output. REPL will be included in node.js from the time of its installation in the computer. Firstly let us understand the working of REPL in node.js with the help of its full form. REPL is abbreviated as Read Eval Print Loop. Let us now understand the working of each of these words one after the other. REPL reads the string of characters given by the user and converts it into Javascript code. After the conversion of data to javascript data structures, it is then stored in the memory. Now comes the part of the eval. Once the reading and conversion of the input data are done, the next part is evaluating it. The javascript data structures are evaluated to get the desired results. The results that we get after evaluation gets printed. After the results get printed, it loops back to the input command. This loop continues its action until ctrl+c is pressed twice which terminates the loop thereby ending it. 

Before getting started with REPL, we have set up a free lab where you can practice and master Node.js.

Getting Started with REPL in Node.js

Before getting started with REPL we should know whether node.js is installed on your device.

The following commands are to be entered:

node -v

Most likely you are going to get the version of the node which is installed in your system, one of which is given below.

v16.13.1

You are going to get the below output if node.js is not installed in your system.

node: command not found

To start REPL, we should simply enter node.js in the command prompt. You will get the result as mentioned in the below line

# >

The above output indicates that the input you give will be executed immediately.

Let us now try some of the arithmetic functions.

# >2+9

You will get the immediate output as shown below

11

Let us now try to create variables in node.js REPL

> let weight = 55

We get the output shown below if we press the enter key

undefined

We get undefined as the output because the statement does not have a return value, but the value of the variable weight is saved until the REPL is exited by using the command .exit or by pressing ctrl+c twice. You can check it by giving the input as shown below:

> weight*5

We get the output as shown below:

275

We can do any number of actions using the weight variable until and unless it is exited by the .exit command or by pressing ctrl+c twice.

Learn Fundamentals of Node.js

Dot commands in node.js REPL

The dot commands in node.js REPL make the tasks easier. The .help command lists all the special commands some of which are shown below:

1.  .break

It aborts the current command

2. .clear

Clears the current terminal and also clear multi-line expression in input given by the user.

3. .exit

Exit from REPL

4. .save

Saves to a file

5. .load

Loads the file related to the current REPL session in node.js

7. .editor

This command enables the user to write multiline code i.e editor mode

Special keys in REPL

1. Tab key

One of the most useful features of node.js REPL is the usage of the tab key. At the time of writing the code, we find the autocompletion of the code which gets printed by pressing the tab key. When it is pressed in an empty line, it displays some of the variables.

2. ctrl+C

It is used as an alternative for the usage of the .break command however pressing twice is an alternative for the .exit command.

3. ctrl+D

An alternative for the usage of the .exit command

Shortcuts in REPL

These are some of the shortcuts which make our work easier

  1. Sometimes there are situations where we need to copy the code that is already written somewhere above. We just need to press the arrow up button, navigating through the upper written code which by default gets copied to the line where the code has to be copied.

  2. The autocorrection feature enables us to complete the full code without actually writing full code with the use of the tab key. Moreover if at all you are not exactly knowing the code, you can just press the tab key twice by which you can get all the possibilities of filling the remaining code. You can choose the desired one out of it.

  3. The REPL commands obviously play a key role in making our work easier.

Using modules in REPL

Modules are the set of written code in javascript which can be used according to your need. Node js has some modules inbuilt in it which can be used without any extra downloading. Some of the modules are shown below:

  • Assert: It gives a set of tests of assertion
  • OS: It prints the information about the operating system of the computer
  • Path: It controls the path of a file
  • Stream: It controls the streaming data
  • zlib: It helps in compressing and decompressing files
  • util: It enables us to the utility functions
  • v8: It enables us to look over the information about v8
  • fs: It enables us to operate the file system
  • domain: To handle errors
  • net: To build clients and servers
  • buffer: To operate binary data
  • http: It helps in making the node.js to an HTTP server
  • https: It helps in making the node.js to an HTTPS server

Use of REPL

The two of the main uses which made the usage of REPL unavoidable are the execution of code and debugging of the code. It helps in debugging as the programmer will be able to evaluate the printed result before deciding what characters to give to the next read. REPL involves more presence of the programmer than the Edit Compile Run Debug which was a classic cycle earlier. REPL allows users to know all the features of the operating system with also providing programming abilities. REPL uses include benchmarking, mathematical calculation, and many more.


User avatar