Skip to the solution
EasyPhone Screen6 min read

Explain the concept of REPL in Node.js

314 views

The problem

Question presented to candidate: "Running node with no file arguments drops you into a prompt where you can type JavaScript directly. What is actually happening there, and is that prompt the same thing as running a .js file?"

What a strong answer should cover:

  • REPL stands for Read-Eval-Print Loop: Node reads one line/expression of input, evaluates it, prints the result, and loops back for the next input β€” running node with no file argument starts this interactive session.
  • It shares Node's actual JavaScript engine and standard library with any script β€” the REPL is not a separate, limited "toy" interpreter; require(), built-in modules, and full language features are all available inside it.
  • πŸ“Œ A concrete, verifiable feature: the REPL keeps a special variable, _, always holding the result of the last evaluated expression β€” genuinely usable in the next line, not just documented behavior.
  • Multi-line input (an unfinished object literal, an open block) is handled by the REPL detecting the incomplete expression and prompting for continuation, rather than evaluating a syntactically broken partial line.
  • Special dot commands exist specifically for the REPL context: .exit, .help, .editor (a multi-line paste-friendly mode), .save/.load (persisting/reloading session history to/from a file) β€” these are REPL-specific, not valid inside an ordinary script.
  • A precise answer distinguishes the REPL's use case (quick, interactive exploration β€” checking how an API behaves, testing a small expression, inspecting a value) from a script file's use case (the actual, repeatable, version-controlled application logic) β€” the REPL is explicitly not the place application code lives.

Clarifying questions expected:

  • "Is the interviewer asking about the interactive CLI experience, or Node's programmatic repl module for building a custom REPL into an application?" β€” both exist and are related but distinct.
  • "Is there a specific dot command or REPL behavior the question is really getting at?"

Code / implementation expected: Optional for the interactive-CLI framing; showing the programmatic repl module actually evaluating input (including the _ variable) is a strong, concrete way to demonstrate real understanding rather than surface familiarity.

  • fundamentals
  • repl
  • cli

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Read the code

JavaScriptA real, programmatic REPL session (repl.start) verifying the special _ last-result variable
const repl = require("repl");
const { PassThrough } = require("stream");

const input = new PassThrough();
const output = new PassThrough();
let collected = "";
output.on("data", (d) => { collected += d.toString(); });

repl.start({ input, output, terminal: false, prompt: "" });

input.write("2 + 2\n");
input.write("_ + 10\n"); // "_" refers to the previous line's result, 4
input.write(".exit\n");

setTimeout(() => console.log(collected), 200);
// 4
// 14   <- proves "_" genuinely held the prior result

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 27 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What is the purpose of `__dirname` and `__filename` in Node.js?

Continue