Open In App
Related Articles

How to use Node.js REPL ?

Improve Article
Save Article
Like Article

Node.Js REPL or Read-Evaluate-Print Loop is an interactive shell for the Node.js environment which means we can write any valid Javascript code in it. This is used to test, evaluate, experiment, or debug code much easier and accessible way. It basically acts as the Browser’s Web dev tools’ Console for Javascript.  

To use the REPL, you must have Node.js downloaded for your Operating System. To check if the Node.Js is installed correctly, you can use the following command:

node --version 

If you get a version number, you are good to go else, you need to fix your installation. So, we can now start to use the node.js REPL in your machine.

How to Start the REPL: To start the Node.js REPL is quite easy and straightforward, you simply have to enter the word node into the Terminal/CMD/PowerShell as per your OS.


You can use any valid Javascript code in the prompt. We do not need to use console.log to print the value of the variables, simply the name of the variables might be sufficient in most cases. 

As we can see the prompt output is a bit more than plain text, it’s nice colored and even has autocompletion built-in. This makes the REPL more convenient and quick to test up some ideas before actually using them in the project.

Exit from the REPL: To exit from the reply, you can press CTRL + D in Windows/Linux and CMD+D in macOS. Optionally, CTRL+C twice will also work to exit. Alternately we can also use the following to exit out of the REPL :


Using Javascript in REPL: We can use any valid javascript in the REPL. We can use variables, strings, concatenation, arithmetic, and all of the stuff that can be feasible in the REPL. There are limitations to what we can write in the REPL, like a bit longer and functional programs. This issue will be seen in the next section of REPL Commands. 

As we can see we have used a couple of concepts in Javascript like string interpolation, arithmetic, and working with arrays. Any valid and feasible Javascript can be used in the REPL and hence some core features of Javascript can be utilized in it.

REPL Commands: There are a couple of commands and arguments to be used in the Node.Js REPL shell. We’ll explore some of them in this section. These commands are to be used in the REPL shell i.e after entering the command node into your terminal/CMD/PowerShell. These commands or characters are reserved in the REPL and hence provide some great features and enhance accessibility. 

Editor command: This command is used to stop the line-by-line evaluation and make an editor-like typing in the shell. It’s nothing like an editor but simply writing more longer and meaningful code as a form of a program.



As we can see we can write more than one line in the shell which makes writing more sophisticated code with a lot of freedom being in the terminal. After writing the required code, we can save and evaluate the code by pressing CTRL + D or we can cancel the evaluation and hence abort the process by pressing CTRL + C. 

Save command: We can use the .save command to save the code of the current REPL session in a file. This might be really handy if you exit the REPL, all the code snippets will be lost and with this command, it becomes much easier to keep a backup at the user’s disposal.



As we can see the code snippet from the REPL is saved into a file.  The file in most cases would be a Js file quite obviously. The .save command is used along with the filename to store the contents of the REPL.

Load  command: The load command as opposed to the .save command loads the variables, functions, and other scopes of a file into the REPL. This is useful to load the existing code from a file for experimenting without re-writing the whole code again.



As we can see, we loaded the file from the previous example, and it crammed it in a single block of code instead of rendering it line by line. We can extend the code as we want and again save it to the file if we want. This makes experimenting much easier and quicker avoiding repetitive writing of code and loading the Js code from a file makes it super useful as well.

Clear command: The .clear command or .break command is used to break from the existing loop statements or multi-line inputs. 



We can see from the above example that after entering the .clear or .break command a new prompt appears and breaks out of the current input or statement These commands do not execute the code and return to the main prompt.

Exit command: As said earlier the alternative to CTRL + D or CTRL + C (twice) is the command .exit. It basically exits the REPL. 


Help command: The help command as stated in the REPL header gives more information about the options and commands available in the Node.Js REPL. 


Underscore Variable: The underscore Variable (_) will give us the result of the last executed command or code. It can be a variable value, function return value, or anything which can return some kind of value, if there is nothing from the evaluation the REPL will default it to undefined.

As we can see in the example, the _ variable gets the result of the last executed command in the shell. It can be undefined if there was just the declaration of a variable else it stores the result or return value of the executed command.

Using Modules: We can even use Js modules in the Node.Js REPL. There are a couple of modules in the Node.Js REPL by default. You can get the list by pressing the TAB key twice. 

If you want to import other modules, you need to follow the following procedure: You need to firstly install the package via the npm package manager for Node.Js.

npm install package_name

After installing the module in the same directory, we can use the “require” command to get the module’s core functionalities. 

As we can see the command:

const express = require('express')

Here, express can be other modules as well. We even use the functions in the modules after writing the boilerplate code for the packages in the REPL. Even we can use a template as a file and load that into a REPL. This makes testing some regularly used modules very easy and quick.

Last Updated : 15 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials