Node.js REPL Module

Java FileNode.js Built-in Modules


REPL isRead-Eval-Print Loopthe abbreviation of (Read-Eval-Print Loop). It is a simple interactive programming environment. In the REPL environment, you can enter code and immediately see the execution results, 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 way:

node

Basic Features of REPL

  1. Read: Reads the JavaScript code entered by the user
  2. Eval: Executes the entered code
  3. Print: Outputs the execution result
  4. Loop: Repeats this process until the user exits

Basic Usage of Node.js REPL

Starting REPL

Type the following directly in the terminalnodecommand to enter the REPL environment:

Example

$ node
Welcome to Node.js v16.14.0.
Type ".help" for more information.
>

Basic Operations

Executing JavaScript Code:

Example

> 1 + 1
2

Defining Variables:

Example

> let x = 10
undefined
> x * 2
20

Multi-line Input: When an incomplete expression is entered, REPL automatically enters multi-line mode:

Example

> function add(a, b) {
... return a + b
... }
undefined
> add(2, 3)
5

Exiting REPL:

  • Press Ctrl+C twice
  • Type.exit
  • Press Ctrl+D

Special Commands in REPL

Node.js REPL provides some special commands that begin with a dot (.):

Common Commands

Command Description
.help Displays all REPL commands
.break Exits multi-line input
.clear Resets the REPL context
.save Saves the current session to a file
.load Loads a session from a file
.editor Enters editor mode

Example

Saving the Current Session:

Example

> .save session.js

Loading a Session:

Example

> .load session.js

Entering Editor Mode:

Example

> .editor
// Enter editor mode, you can input multiple lines of code
// Press Ctrl+D to execute the code

Advanced Features of REPL

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 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';

You can directly use these variables and functions 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 Use Cases of REPL

1. Quickly Test Code Snippets

REPL is the fastest way to test JavaScript code snippets without needing to create files.

2. Learning and Experimenting

It is very suitable for learning new JavaScript features or Node.js APIs.

3. Debugging

You can quickly check variable values or test function behavior in REPL.

4. Prototyping

In the early stages of development, you can use REPL to quickly validate ideas.


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 the Tab key 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, using the.editorcommand is more convenient.


Summary

The Node.js REPL module is a powerful interactive programming environment. It:

  1. Provides the ability to quickly test JavaScript code
  2. Supports custom configuration and extensions
  3. Includes many useful built-in commands and features
  4. Can be used for learning, debugging, and prototyping

Whether you are a beginner or an experienced developer, REPL is a very valuable tool and is worth mastering.

Java FileNode.js Built-in Modules

Other Extensions