Node.js REPL runs JavaScript code in an instant test space inside your terminal.
Table of Content
What is Node.js REPL?
Node.js REPL is a console inside Node.js that reads code, evaluates it, prints the result, and waits for the next input.
The REPL reads code line by line and returns the output after each line. It gives you history for commands and direct access to Node.js modules. It also lets you use JavaScript objects and methods fast.
You can open your terminal and type node , then press enter. The prompt > appears and waits for your input.
You can write JavaScript code and press Enter to see the result at once.
REPL does not store your work after exit. It runs code line by line but cannot run files. It cannot handle heavy apps because it is for small tests only.
The Common Commands in REPL
Here are some useful commands inside Node.js REPL. Each command has a small example.
- .help
- .exit
- .save
- .load
Let’s explain each one in depth:
The .help shows you all available REPL commands. Here is an example:
.helpIt prints a list of all REPL commands you can use.
The .exit closes the REPL session. For example:
.exitIt stops the current REPL session and returns to your terminal.
The .save saves your REPL session into a file. For example:
.save session.jsIt writes all commands from the current REPL session into session.js.
The .load loads code from a file into REPL. Here is an example:
.load session.jsIt loads all code from session.js into your current REPL session.
Node.js REPL vs Console
Node.js REPL runs live code inside a terminal. It gives instant feedback for each line. The console object in Node.js writes logs or errors from running code inside files or servers.
REPL helps developers test code blocks fast. The console object helps apps log data, debug, or show user messages. REPL works inside your terminal. Console works inside your program.
Use REPL for small experiments and test ideas, and the console for logs and debug data inside real apps.
Examples
Math in REPL:
2 + 3This code returns 5 inside REPL at once. It shows how REPL handles direct math without extra steps.
Load a Module:
const os = require('os')
os.platform()This code imports the os module and shows the platform name inside REPL. It helps you check system details in one step.
Save a Session:
.save mysession.jsThis code writes all commands from your current REPL session into a file named mysession.js for later use.
Load a Saved Session:
.load mysession.jsThis code loads a saved REPL session back into the console so you can continue from the same point.
Wrapping Up
You learned what Node.js REPL is and how it works. You also saw how to start it, use common commands, and compare it with the console.
Here is a quick recap:
- Node.js REPL runs live code inside your terminal.
- It gives instant results.
- You can save sessions, load files, and use built-in modules.
- The console object logs data inside real apps.
- Use REPL for tests and console for app logs.
FAQs
What is Node.js REPL and how does it work?
- Type JavaScript code directly
- Execute it instantly
- See the results in real-time
nodeHow do you use Node.js REPL commands?
.help→ Show available commands.exit→ Exit the REPL.clear→ Reset REPL context.save filename.js→ Save session.load filename.js→ Load file into REPL
> .help
> .exit
What are the benefits of using Node.js REPL?
- Quick Testing: Run code instantly without a file
- Debugging: Inspect values and test snippets
- Learning: Great tool for beginners
- Prototyping: Experiment before writing full code
> 2 + 3
5
How to run JavaScript expressions in Node.js REPL?
> let x = 10;
> let y = 20;
> x + y
30
Inline example:
Use console.log(x * y) inside REPL to print values. Similar Reads
Installing node js on Windows, Ubuntu, or macOS. You need to pick the version of Node.js that you want to…
Node Package Manager (NPM) is an essential tool at the heart of the Node.js ecosystem. This is the defacto package…
This guide shows Node.js modules, how they work, how to import and export them, and why you use them. What…
Node JS Buffers store raw binary data in memory. This guide shows how to create them, work with them, and…
This article shows how Node.js creates HTTP servers. It covers requests, responses, routes, HTML, JSON, and gives examples for both…
NVM (Node Version Manager ) is a command-line tool used to manage multiple versions of Node.js on a single machine.…
Streams pass data in parts, so the app stays fast. This article explains Node.js Streams and how to use them.…
In this quick tutorial, you will start Node.js tutorials with a Hello World program. You will learn how to set…
Node JS runs code in an event-based model and uses events to drive tasks. An event is a signal that…
Node.js file system lets you read, write, update, and manage files and folders with built-in methods. Understand the Node.js File…