- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js REPL (Read-Eval-Print Loop)
The Node.js REPL (Read-Eval-Print Loop) is an interactive shell that allows you to execute JavaScript code directly in your terminal. Think of it as a "playground" or a "sandbox" for backend development—similar to the Developer Console in Chrome or Firefox, but for the Node.js environment. It is an essential tool for rapidly testing logic, debugging small snippets of code, and exploring Node's built-in modules without having to create and run a .js file.
Key Features of REPL
The acronym REPL describes exactly how the environment handles your code in four distinct steps:
1. Read
- The REPL waits for the user to type an expression and reads the input into memory.
- Example: You type a simple arithmetic operation.
> 5 + 10
2. Eval
- The Node.js engine takes the input and evaluates (executes) it.
- It processes the logic—in this case, adding two numbers together.
3. Print
- The result of the evaluation is displayed immediately in the terminal.
15
undefined printed after a console.log(). This happens because REPL prints the return value of every statement. Since console.log returns undefined, you see both the message and the return value.
4. Loop
- The environment resets the prompt and waits for the next input, allowing for a continuous, interactive session.
Starting the REPL
1. Launch REPL
- To start the interactive shell, simply open your terminal (Command Prompt, PowerShell, or Bash) and type
nodewithout any arguments:
node
- If Node is installed correctly, you will see the REPL prompt, which looks like a "greater-than" sign:
>
2. Execute Commands
- You can now type any valid JavaScript. For example, try printing a string to the console:
> console.log("Hello, Node.js REPL!");
Output:
Hello, Node.js REPL!
undefined
.js file.
Useful Commands in REPL
1. Perform Calculations
The REPL can be used as a powerful calculator that supports all JavaScript Math functions and operators.
> 10 * 3
30
> Math.sqrt(64)
8
_ variable to get the result of the last expression evaluated. For example, if you just typed 10 * 3, typing _ + 5 will result in 35.
2. Define Variables
Variables defined in the REPL session stay in memory for the duration of that session.
> let x = 20;
> const y = 5;
> x + y;
Output:
25
3. Multiline Input
Node.js REPL supports multiline blocks like functions, loops, and objects. When you press Enter after an opening brace {, the prompt changes to three dots ..., indicating that Node is waiting for you to finish the block.
> function greet(name) {
... return `Hello, ${name}!`;
... }
> greet("Developer")
Output:
'Hello, Developer!'
4. Access Global Variables
You can check the available global objects in the Node environment, such as process or module.
> process.version
'v18.16.0'
__dirname and __filename are not available in the REPL. If you try to access them, you will get a ReferenceError because the REPL is not running within a specific file context.
5. Exit REPL
- To close the REPL, you can type
.exitand hit Enter. - Alternatively, press
Ctrl+Ctwice, orCtrl+Donce.
> .exit
REPL Shortcuts
Summary
The Node.js REPL is an incredibly versatile tool for any developer's workflow. It provides an immediate feedback loop that is perfect for experimentation and learning. Whether you are checking the output of a complex array transformation or testing how a specific Node module works, the REPL saves you the overhead of creating temporary files. Start incorporating it into your daily coding routine to speed up your prototyping and debugging process!