Nodejs Repl Module
[Node.js Built-in Modules](#)
* * *
REPL stands for **Read-Eval-Print Loop**, which is a simple interactive programming environment. In a REPL environment, you can input code and immediately see the execution results, making it perfect for quickly testing code snippets and learning new concepts.
Node.js has a built-in REPL environment that you can start in the following way:
node
### Basic Functions of REPL
1. **Read**: Read user-input JavaScript code
2. **Eval**: Execute the input code
3. **Print**: Output the execution results
4. **Loop**: Repeat this process until the user exits
* * *
## Basic Usage of Node.js REPL
### Starting REPL
Simply type the `node` command in the terminal 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 variables**:
## Example
> let x =10
undefined
> x *2
20
**Multi-line input**: When you enter an incomplete expression, REPL automatically enters multi-line mode:
## Example
>function add(a, b){
... return a + b
... }
undefined
> add(2,3)
5
**Exit REPL**:
* Press Ctrl+C twice
* Type `.exit`
* Press Ctrl+D
* * *
## Special REPL Commands
Node.js REPL provides some special commands that start with a dot (.):
### Common Commands
| Command | Description |
| --- | --- |
| .help | Display all REPL commands |
| .break | Exit multi-line input |
| .clear | Reset REPL context |
| .save | Save current session to file |
| .load | Load session from file |
| .editor | Enter editor mode |
### Examples
**Save current session**:
## Example
> .save session.js
**Load session**:
## Example
> .load session.js
**Enter editor mode**:
## Example
> .editor
// Enter editor mode, can input multi-line code
// Press Ctrl+D to execute code
* * *
## Advanced REPL Features
### Custom REPL Environment
You can create a custom REPL environment programmatically:
## Example
const repl = require('repl');
// Create custom REPL
const myRepl = repl.start({
prompt:'MyNode> ',
useColors:true
});
// Add custom command
myRepl.defineCommand('sayhello',{
help:'Say hello',
action(name){
this.clearBufferedCommand();
console.log(`Hello, ${name ||'stranger'}!`);
this.displayPrompt();
}
});
### REPL Context
You can pre-add variables or functions to the REPL context:
## 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 REPL:
## Example
MyNode> myVar
'Hello from context'
MyNode> myFunc()
'Hello from function'
### REPL Server
You can even create a network 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 Applications of REPL
### 1. Quick Code Testing
REPL is the fastest way to test JavaScript code snippets without creating files.
### 2. Learning and Experimentation
Perfect for learning new JavaScript features or Node.js APIs.
### 3. Debugging
Quickly check variable values or test function behavior in REPL.
### 4. Prototype Development
Use REPL to quickly validate ideas in the early stages of development.
* * *
## REPL Usage Tips
### 1. Using the _ Variable
REPL stores the result of the previous 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 up and down arrow keys to browse command history.
### 4. Editor Mode
For complex multi-line code, use the `.editor` command for convenience.
* * *
## Summary
Node.js REPL module is a powerful interactive programming environment that:
1. Provides the ability to quickly test JavaScript code
2. Supports custom configuration and extension
3. Includes many useful built-in commands and features
4. Can be used for learning, debugging, and prototype development
Whether you are a beginner or experienced developer, REPL is a valuable tool worth mastering.
[Node.js Built-in Modules](#)
YouTip