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 use Sequelize in Node.js ?
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. Its features are solid transaction support, relations, eager and lazy loading, read replication and many more. Features of Sequelize: Sequelize is a third-party package to be precise its an Objec
2 min read
How to Return JSON using Node.js ?
Node.js is a powerful platform for building server-side applications and returning JSON data is a common task in web development, especially when building APIs. JSON stands for JavaScript Object Notation. It is one of the most widely used formats for exchanging information across applications. Node.
4 min read
How to use SolverJS ?
SolverJS is a JavaScript library, it contains a lot of mathematical and basic logical function which is used very frequently in any application. It aims at solving logical problems easily with a simple call to certain functions also all functions are time and space-efficient. It has a variety of use
10 min read
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 jQuery with Node.js ?
jQuery is a popular JavaScript library primarily used for client-side scripting to simplify HTML document traversal, event handling, animation, and AJAX interactions. Although jQuery is designed for the browser, you might find scenarios where you want to use it on the server side with Node.js, such
3 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 read and write JSON file using Node ?
Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises. JSON(JavaScript Object Notation) is a simple and
3 min read
Node.js os.EOL
The os.EOL constant is an inbuilt application programming interface of the os module which is used to get end-of-line character or marker as specified by the operating system. Syntax: os.EOL Return Value: It returns the EOL (end-of-line marker) as specified by the operating system on which it is run
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