What is the Node.js REPL?
Node.js comes with a built-in REPL (Read-Eval-Print Loop) environment that allows you to execute JavaScript code interactively. The REPL is accessible through the terminal and is a great way to test out small pieces of code.REPL stands for Read Evaluate Print Loop. It is a programming language environment (basically a console window) that takes a single expression as user input and returns the result back to the console after execution. The REPL session provides a convenient way to quickly test simple JavaScript code.Starting the REPL
Thenode command is used to run Node.js scripts:
node command without any script or arguments, it starts a REPL session:
Using the REPL
The REPL waits for you to enter JavaScript code. Start with something simple:test, is the output printed to the console. Then undefined is the return value of running console.log(). Node read this line of code, evaluated it, printed the result, and then went back to waiting for more lines. Node loops through these three steps for every piece of code executed in the REPL until you exit the session — that is where the REPL got its name.
Node automatically prints the result of any line of JavaScript code without the need to instruct it to do so:
The REPL printed
undefined after executing console.log(), while it printed the result directly for 5 === '5'. The former is a statement; the latter is an expression.Multi-line mode
In some cases, code you want to test might need multiple lines. The REPL is smart enough to detect this automatically. For example, start typing a function definition and press enter:Special features
The _ special variable
If after some code you type _, it prints the result of the last operation.
The up arrow key
Pressing theup arrow key gives you access to the history of the previous lines of code executed in the current, and even previous, REPL sessions.
Dot commands
The REPL has special commands, all starting with a dot (.):
| Command | Description |
|---|---|
.help | Shows the dot commands help |
.editor | Enables editor mode to write multiline JavaScript code with ease. Press ctrl-D to run the code. |
.break | When inputting a multi-line expression, aborts further input. Same as pressing ctrl-C. |
.clear | Resets the REPL context to an empty object and clears any multi-line expression currently being input. |
.load | Loads a JavaScript file relative to the current working directory |
.save | Saves all you entered in the REPL session to a file (specify the filename) |
.exit | Exits the REPL (same as pressing ctrl-C two times) |
.editor. For example, if you start typing an iteration:
enter, the REPL goes to a new line starting with 3 dots, indicating you can continue working on that block:
.break at the end of a line, multi-line mode will stop and the statement will not be executed.
Run REPL from a JavaScript file
You can import the REPL module in a JavaScript file to start a REPL session programmatically:- ESM
- CJS
'> ' (with a trailing space), but you can define a custom prompt: