Open In App

NodeJS REPL (READ, EVAL, PRINT, LOOP)

Last Updated : 24 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

NodeJS REPL (Read-Eval-Print Loop) is an interactive shell that allows you to execute JavaScript code line-by-line and see immediate results. This tool is extremely useful for quick testing, debugging, and learning, providing a sandbox where you can experiment with JavaScript code in a NodeJS environment.

In this article, we’ll explore what the NodeJS REPL is, how it works, its key features, and how you can use it effectively for testing and debugging.

What is REPL?

REPL is like a JavaScript playground in your terminal. If you type the code, REPL runs it, shows you the result, and then waits for your next command. It’s a loop:

  • READ: You type some JavaScript code into the terminal, and REPL reads what you typed.
  • EVAL: REPL runs (evaluates) your code.
  • PRINT: REPL shows you the result of your code.
  • LOOP: REPL goes back to step 1, waiting for you to type more code. This loop continues until you quit REPL.

Getting Started with REPL

To start working with the REPL environment of NodeJS, follow one of these two methods:

Starting REPL in the Terminal or Command Prompt

  • Open your terminal (for UNIX/Linux) or Command Prompt (for Windows).
  • Type node and press ‘Enter’ to start the REPL.
node
open node repl gfg

open node repl

The REPL has started and is demarcated by the ‘>’ symbol. Various operations can be performed on the REPL. Below are some of the examples to get familiar with the REPL environment.

Key Features of NodeJS REPL

Executing JavaScript Code

The REPL is a full-featured JavaScript environment, meaning you can run any valid JavaScript code inside it.

Example:

> const x = 10;> const y = 20;> x + y30
  • You can declare variables, create functions, and run any code that would work in a regular JavaScript runtime.

Multi-Line Input

In case of complex logic (like loops or functions), the REPL supports multi-line input. When you enter a block of code, the REPL will continue reading input until the block is complete.

Example:

> function add(a, b) {...   return a + b;... }> add(5, 10)15
  • Here, the REPL waits for you to complete the function block before evaluating the code.

Underscore (_) Variable

The REPL provides a special variable _ (underscore) that stores the result of the last evaluated expression.

Example:

> 3 + 36> _ * 212
  • In this case, the result of 3 + 3 is stored in _, which is then used in the next line to calculate 12.

Saving and Loading REPL Sessions

The REPL allows you to save the session output to a file and load previously saved sessions, making it easier to keep track of the code you’ve tested.

Saving a Session: To save your REPL session to a file, use the .save command:

> .save ./repl_session.js

Loading a Session: You can load the saved session into a new REPL session using .load:

> .load ./repl_session.js

Accessing NodeJS Core Modules

The REPL environment allows you to load and use NodeJS core modules, such as fs, path, http, etc., without needing to exit the REPL or write an entire script.

Example:

> const fs = require('fs');> fs.readFileSync('test.txt', 'utf8');
  • In this example, the fs (file system) module is loaded, and the REPL reads the content of a file named test.txt.

Error Handling in REPL

The REPL is forgiving of errors and will display helpful error messages without crashing the entire session. This makes it a safe environment for testing.

Example:

   > console.log(foo);ReferenceError: foo is not defined

Built-in REPL Commands

NodeJS REPL provides several built-in commands (REPL commands always start with a dot .).

  • .help: Displays a list of all available REPL commands.
  • .break: Breaks out of multi-line input or clears the current input.
  • .clear: Resets the REPL context by clearing all declared variables.
  • .exit: Exits the REPL session.

Arithmetical operations in REPL

simple math arithmetic gfg

Arithmetical operations in REPL

Operations using libraries of NODE

Math library methods gfg

Math library methods gfg

  • Note: using ‘math’ shows error as the library is referenced as ‘Math’ in NODE and not ‘math’.

Using variables in REPL

The keyword var is used to assign values to variables.

variable assignment gfg

Using Variables in REPL

Using loops in REPL

Loops can be used in REPL as in other editors.

multiline expression gfg

  • Note: Use ctrl – c to terminate the command and ctrl – c twice to terminate the NODE REPL.

.help is used to list out all the commands.

repl help gfg

Using .help in REPL

Best Practices for NodeJs REPL

  • Use REPL for Quick Testing: Utilize the REPL to test small code snippets or experiment with new features without creating a separate file.
  • Leverage Built-in Modules: Access and test NodeJS built-in modules directly in the REPL to understand their functionality and behavior.
  • Save and Load Code with .save and .load: Continue your REPL sessions by saving code to a file and loading it back later for continued work or testing.
  • Use for Prototyping and Debugging: Quickly test small code snippets or debug specific functionality by running them interactively in the REPL.

NodeJS REPL (READ, EVAL, PRINT, LOOP) – FAQs

What is the NodeJS REPL?

The NodeJS REPL is an interactive shell that reads, evaluates, prints, and loops, allowing for immediate execution of JavaScript code.

How do I start the NodeJS REPL?

Open your terminal or command prompt, type node, and press Enter to start the REPL session.

Can I use NodeJS modules in the REPL?

Yes, you can require and use NodeJS modules directly within the REPL. For example, const fs = require(‘fs’); allows you to use the file system module.

How do I exit the REPL?

To exit the REPL, type .exit or press Ctrl+C twice.

Is the REPL suitable for debugging?

While the REPL is useful for quick tests and experiments, for more complex debugging tasks, consider using NodeJS’s built-in debugger or external debugging tools.



Next Article

Similar Reads

three90RightbarBannerImg