Skip to content

Node.js REPL Terminal

REPL stands for Read-Eval-Print Loop. It’s an interactive programming environment, like a command prompt or shell, where you can type in commands, have them evaluated immediately, see the output, and then repeat the process. Node.js comes bundled with its own REPL environment, which is incredibly useful for experimenting with JavaScript code, testing Node.js features, debugging small snippets, and learning.

The REPL cycle involves these steps:

  • Read: Reads the user’s input (a line of JavaScript code).
  • Eval: Evaluates the input code using the Node.js runtime.
  • Print: Prints the result of the evaluation to the console.
  • Loop: Waits for the next line of input from the user. This continues until the user exits the REPL (typically by pressing Ctrl+C twice or typing .exit).

To start the Node.js REPL, simply open your terminal or command prompt and type node, then press Enter:

$ node

You’ll be greeted with the REPL prompt (>), indicating it’s ready to accept your JavaScript commands:

Welcome to Node.js vXX.X.X. (Current Node version will be displayed)
Type ".help" for more information.
>

You can immediately start typing JavaScript expressions:

> 5 + 7
12
> 'Hello' + ' ' + 'REPL!'
'Hello REPL!'
> 10 * (3 + 4)
70
> Math.random()
0.123456789... (a random number)

You can declare and use variables just like in a script. Using const or let (or var, though less common now) declares the variable. If you assign a value without const/let/var, Node.js implicitly treats it as a global variable assignment (in REPL context) and prints the assigned value. Using const/let/var usually results in undefined being printed for the declaration line itself.

> const message = 'Welcome'
undefined
> let count = 10
undefined
> count * 5
50
> message + ' to Node.js REPL'
'Welcome to Node.js REPL'
> // Implicit global assignment (less recommended)
> myVar = 100
100
> myVar / 4
25
> console.log('Logging works too:', count)
Logging works too: 10
undefined

The REPL supports multiline expressions. When you start a block (e.g., with {, (, or [ ) or end a line where more input is expected, the prompt changes to ... indicating it’s waiting for more lines.

> function greet(name) {
... console.log(`Hello, ${name}!`);
... }
undefined
> greet('Node User')
Hello, Node User!
undefined
>
> let numbers = [1, 2, 3];
undefined
> numbers.map(n => {
... return n * 2;
... });
[ 2, 4, 6 ]
>

The ... continues until you close the block or enter a complete statement.

The special variable _ (underscore) holds the result of the last successfully evaluated expression.

> 10 + 15
25
> _ * 2 // Use the last result (25)
50
> const names = ['Alice', 'Bob']
undefined
> names
[ 'Alice', 'Bob' ]
> _[1] // Use the last result (the array) to access 'Bob'
'Bob'
>

The REPL has several special commands, all starting with a dot (.):

  • .help: Display help message listing available dot commands.
  • .break: (or Ctrl+C once) Abort the current multiline expression input.
  • .clear: Resets the REPL context to an empty object and clears any multiline expression. Same effect as .break.
  • .exit: (or Ctrl+C twice, or Ctrl+D) Exit the Node.js REPL.
  • .save <filename>: Save the current REPL session history to a file.
  • .load <filename>: Load JavaScript code from a file into the current REPL session.
  • .editor: Enter editor mode for writing longer blocks of code. Ctrl+D finishes, Ctrl+C cancels.
  • Up/Down Arrow Keys: Navigate through command history.
  • Tab Key: Autocomplete variables, keywords, and object properties.
  • Ctrl+L: Clear the terminal screen (like console.clear()).

As mentioned, you can exit the REPL using:

  • Typing .exit and pressing Enter.
  • Pressing Ctrl+C twice.
  • Pressing Ctrl+D (on an empty line).
> // Press Ctrl+C
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
> // Press Ctrl+C again
$