What is REPL?
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:
Most likely you are going to get the version of the node which is installed in your system, one of which is given below.
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.
You will get the immediate output as shown below
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
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:
We get the output as shown below:
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.
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:
It aborts the current command
Clears the current terminal and also clear multi-line expression in input given by the user.
Exit from REPL
Saves to a file
Loads the file related to the current REPL session in node.js
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.
It is used as an alternative for the usage of the .break command however pressing twice is an alternative for the
An alternative for the usage of the
Shortcuts in REPL
These are some of the shortcuts which make our work easier
- 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.
- 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.
- The REPL commands obviously play a key role in making our work easier.
Using modules in REPL
- 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.