Node.js REPL: From Basics to Advanced
Node.js REPL (Read-Eval-Print Loop) is an interactive shell that allows you to run JavaScript code directly in a Node.js environment. This powerful tool is invaluable for debugging, prototyping, and learning JavaScript.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler
Starting the Node.js REPL
To launch the Node.js REPL, open your terminal or command prompt and type node followed by the Enter key. You should now see a > prompt, indicating that you're in the REPL.
$ node
>Basic Usage
Type JavaScript code directly into the REPL, and it will execute the code and print the result.
> const x = 5;
undefined
> const y = 7;
undefined
> console.log(x + y);
12
undefinedMultiline Input
If you need to write a multiline expression, simply press Enter after each line. The REPL will automatically recognize when the expression is complete.
> function greet(name) {
... return `Hello, ${name}!`;
... }
undefined
> greet('Alice');
'Hello, Alice!'Get JavaScript-World’s stories in your inbox
Join Medium for free to get updates from this writer.
Using the Underscore Variable
The result of the last executed expression is stored in the _ (underscore) variable. You can use this variable in subsequent expressions.
> const z = _ * 2;
24
> console.log(z);
24
undefinedLoading External Modules
You can also load external modules in the REPL using require().
> const fs = require('fs');
undefinedSaving and Loading REPL Sessions
To save your current REPL session to a file, use the .save command followed by the file path.
> .save my-session.js
Session saved to: my-session.jsTo load a saved REPL session, use the .load command followed by the file path.
> .load my-session.js
Session loaded from: my-session.jsThe Node.js REPL is a powerful tool for debugging, prototyping, and learning JavaScript. As you become more familiar with its features and capabilities, it will become an indispensable part of your Node.js development workflow.
If you enjoyed the article and would like to show your support, be sure to:
👏 Applaud for the story (50 claps) to help this article get featured
👉Follow me on Medium
Check out more content on my Medium profile






