Node.js REPL Terminal
Node.js - REPL Terminal
Section titled “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+Ctwice or typing.exit).
Starting the REPL
Section titled “Starting the REPL”To start the Node.js REPL, simply open your terminal or command prompt and type node, then press Enter:
$ nodeYou’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.>Simple Expressions
Section titled “Simple Expressions”You can immediately start typing JavaScript expressions:
> 5 + 712> 'Hello' + ' ' + 'REPL!''Hello REPL!'> 10 * (3 + 4)70> Math.random()0.123456789... (a random number)Using Variables
Section titled “Using Variables”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 = 10undefined> count * 550> message + ' to Node.js REPL''Welcome to Node.js REPL'> // Implicit global assignment (less recommended)> myVar = 100100> myVar / 425> console.log('Logging works too:', count)Logging works too: 10undefinedMultiline Expressions
Section titled “Multiline Expressions”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.
Underscore (_) Variable
Section titled “Underscore (_) Variable”The special variable _ (underscore) holds the result of the last successfully evaluated expression.
> 10 + 1525> _ * 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'>REPL Commands
Section titled “REPL Commands”The REPL has several special commands, all starting with a dot (.):
.help: Display help message listing available dot commands..break: (orCtrl+Conce) 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: (orCtrl+Ctwice, orCtrl+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+Dfinishes,Ctrl+Ccancels.
Other Useful Keystrokes:
Section titled “Other Useful Keystrokes:”Up/Down Arrow Keys: Navigate through command history.Tab Key: Autocomplete variables, keywords, and object properties.Ctrl+L: Clear the terminal screen (likeconsole.clear()).
Stopping the REPL
Section titled “Stopping the REPL”As mentioned, you can exit the REPL using:
- Typing
.exitand pressing Enter. - Pressing
Ctrl+Ctwice. - 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$