MyInternships.in

Node.js Basics

Node.js REPL

Typing node with no arguments opens a Read-Eval-Print Loop: an interactive shell that reads an expression, evaluates it, prints the result and loops. It is the fastest way to test a snippet, explore an unfamiliar module or check what a function actually returns, with no file to create and delete.


What is REPL in Node.js?

Typing node with no arguments opens a Read-Eval-Print Loop: an interactive shell that reads an expression, evaluates it, prints the result and loops. It is the fastest way to test a snippet, explore an unfamiliar module or check what a function actually returns, with no file to create and delete.

Why REPL matters

Most "does this method return an array or an object?" questions take five seconds in the REPL and five minutes if you write a script for them. It is also how you inspect a running server’s modules without touching the codebase.

REPL example

A typical REPL session
Terminal
$ node
Welcome to Node.js v22.
> const fs = require('node:fs')
undefined
> fs.readdirSync('.')
[ 'package.json', 'src', 'node_modules' ]
> _.length            // _ holds the previous result
3
> await fetch('https://api.github.com').then(r => r.status)
200
> .exit

How this works

Each line is evaluated immediately and its value printed — which is why const declarations print undefined. The special variable _ holds the last result, so you can chain exploratory steps without retyping.

REPL dot-commands

Terminal
.help          list every command
.editor        multi-line editing mode (Ctrl+D to run)
.save out.js   write this session's history to a file
.load in.js    execute a file inside the current session
.break         abandon a half-typed multi-line expression
.clear         reset the REPL context
.exit          quit (or press Ctrl+D)

Key points to remember

  • _ holds the value of the last evaluated expression.
  • Tab completion works on object properties, so fs.<Tab> lists the whole API.
  • Top-level await works, which makes testing promise-based code straightforward.
  • .editor mode lets you paste or write multi-line functions without the REPL executing each line.
  • History persists between sessions in ~/.node_repl_history.

Ways to run Node code, and when to use each

CommandUse for
nodeinteractive exploration and quick checks
node app.jsrunning a real script
node --watch app.jsdevelopment, restarting on save
node -e "code"a one-liner from a shell script or CI
node -p "code"a one-liner whose result is printed

Common mistakes with REPL

  • Pasting a multi-line function without .editor, so the REPL evaluates each line separately and errors.
  • Expecting variables from the REPL to be visible to a separate node process — each session is isolated.
💡

node -p "require('./package.json').version" prints a value straight from the shell, which is handy inside deploy scripts and CI steps.

Node.js REPL— Interview Questions & FAQs

What is the Node.js REPL used for?+

Quick experiments — checking what a function returns, exploring a module’s API, or testing a regular expression — without creating and running a file.

How do I paste multi-line code into the Node REPL?+

Type .editor first. That switches to multi-line mode where nothing is evaluated until you press Ctrl+D, so functions and blocks paste correctly.

Related Node.js Topics

Keep learning with these closely related lessons.

Ready to use your Node.js skills?

Find verified Node.js internships and fresher developer jobs across India.

Browse Node.js Internships