Node.js REPL module
REPL is Read-Eval-Print Loopan abbreviation for (Read-Eval-Print Loop), a simple interactive programming environment. In a REPL environment, you can type code and immediately see the result, making it ideal for quickly testing code snippets and learning new concepts.
Node.js has a built-in REPL environment, which you can start in the following ways:
node
Basic REPL features
- Read: read JavaScript code entered by the user
- Eval: execute the entered code
- Print: output the execution result
- Loop: repeat this process until the user exits
Basic usage of Node.js REPL
Start the REPL
Type the following directly in the terminal node command to enter the REPL environment:
Example
$ node
Welcome to Node.js v16.14.0.
Type
".help"
for
more
information.
Basic operations
Execute JavaScript code:
Example
1
1
2
Define a variable:
Example
let x
=
10
undefined
x
2
20
Multi-line input: when an incomplete expression is entered, the REPL automatically switches to multi-line mode:
Example
function
add
(
a
,
b
)
{
...
return
a
b
...
}
undefined
add
(
2
,
3
)
5
Exit the REPL:
- Press Ctrl+C twice
- Input
.exit - Press Ctrl+D
Special REPL commands
Node.js REPL provides several special commands that start with a dot (.):
Common Commands
| Command | Description |
|---|---|
| .help | Display all REPL commands |
| .break | Exit multi-line input |
| .clear | Reset the REPL context |
| .save | Save the current session to a file |
| .load | Load a session from a file |
| .editor | Enter editor mode |
Example
Save the current session:
Example
.
save
session.
js
Load a session:
Example
.
load
session.
js
Enter editor mode:
Example
.
editor
// Enter editor mode to type multiple lines of code
// Press Ctrl+D to execute the code
Advanced REPL features
Custom REPL environment
You can create a custom REPL environment programmatically:
Example
const
repl
=
require
(
'repl'
)
;
// Create a custom REPL
const
myRepl
=
repl.
start
(
{
prompt
:
'MyNode> '
,
useColors
:
true
}
)
;
// Add custom commands
myRepl.
defineCommand
(
'sayhello'
,
{
help
:
'Say hello'
,
action
(
name
)
{
this
.
clearBufferedCommand
(
)
;
console.
log
(
`Hello
,
$
{
name
||
'stranger'
}
!
`
)
;
this
.
displayPrompt
(
)
;
}
}
)
;
REPL context
You can add variables or functions to the REPL context in advance:
Example
const
repl
=
require
(
'repl'
)
;
const
r
=
repl.
start
(
)
;
r.
context
.
myVar
=
'Hello from context'
;
r.
context
.
myFunc
=
(
)
=>
'Hello from function'
;
These variables and functions can be used directly in the REPL:
Example
MyNode
myVar
'Hello from context'
MyNode
myFunc
(
)
'Hello from function'
REPL server
You can even create a networked REPL server:
Example
const
net
=
require
(
'net'
)
;
const
repl
=
require
(
'repl'
)
;
net.
createServer
(
(
socket
)
=>
{
repl.
start
(
{
prompt
:
'Remote> '
,
input
:
socket
,
output
:
socket
,
terminal
:
true
}
)
.
on
(
'exit'
,
(
)
=>
{
socket.
end
(
)
;
}
)
;
}
)
.
listen
(
8080
)
;
Practical REPL use cases
1. Quickly test code snippets
REPL is the fastest way to test JavaScript code snippets without creating a file.
2. Learning and experimentation
It is great for learning new JavaScript features or Node.js APIs.
3. Debugging
You can quickly check variable values or test function behavior in the REPL.
4. Prototyping
In the early stages of development, you can use the REPL to quickly validate ideas.
REPL tips
1. Using the _ variable
The REPL stores the result of the last expression in the _ _ variable:
Example
2
2
4
_
3
12
2. Auto-completion
Press Tab to auto-complete variable names, object properties, and global objects.
3. Command history
Use the up and down arrow keys to browse command history.
4. Editor mode
For complex multi-line code, it is more convenient to use the .editor command.
Summary
Node.js's REPL module is a powerful interactive programming environment. It:
- provides the ability to quickly test JavaScript code
- supports custom configuration and extension
- includes many useful built-in commands and features
- can be used for learning, debugging, and prototyping
Whether you are a beginner or an experienced developer, REPL is a very valuable tool worth mastering.
Node.js Built-in Modules