How to use Node.js REPL ?
Last Updated : 15 Feb, 2022
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.
node
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 :
.exit
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.
Similar Reads
How to Send JSON Response using Node.js ?
NodeJS is the runtime environment, which can execute the javascript code on any platform. It is widely used to create and run web application servers because of its salient features. During production, several times we need to send the resources or some type of information as a response, and javascr
5 min read
How to use Superheroes Module in Node.js ?
The superheroes module is a fun and simple Node.js package that allows you to generate random superhero names. It's a great example of how to use third-party modules in Node.js and can be useful for testing, generating sample data, or just adding some flair to your applications. In this article, we'
2 min read
How to Setup View Engine in Node.js ?
View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with
2 min read
How to Take Input in Node.js ?
Taking input in a Node.js application is essential for building interactive command-line interfaces, processing user input, and creating dynamic applications. Node.js provides several methods for receiving input from users, including reading from standard input (stdin), command-line arguments, and u
2 min read
What is NPM & How to use it ?
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
3 min read
How to Run Node Server?
A Node server runs JavaScript outside the browser to handle web requests. It listens for incoming requests, processes them, and sends responses. Unlike traditional servers, it handles multiple requests at once without waiting for each to finish. Some of the key features of the Node Server are: Non-B
3 min read
How to Build a Simple Web Server with Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read
How to refresh a file in Node.js ?
Node.js has seen an important growth in past years and is still increasing its value in many organizations and business models. Companies like Walmart or PayPal have already started to adopt it. NPM, the package manager of Node.js has been already installed when you install Node.js and is ready to r
2 min read
Node.js Projects
Node.js is one of the most popular JavaScript runtime environments widely used in the software industry for projects in different domains like web applications, real-time chat applications, RESTful APIs, microservices, and more due to its high performance, scalability, non-blocking I/O, and many oth
9 min read
How to Install Node.js on Windows
Installing Node.js on Windows is a straightforward process, but it's essential to follow the right steps to ensure smooth setup and proper functioning of Node Package Manager (NPM), which is crucial for managing dependencies and packages. This guide will walk you through the official site, NVM, Wind
6 min read