How to create and use a custom REPL

Node.js allows users to create their own REPLs with the repl module. Its basic use looks like this:

const repl = require('repl');

repl.start(prompt, stream);

Above, prompt is a string that's used for the prompt of your REPL (which defaults to "> ") and stream is the stream that the repl listens on, defaulting to process.stdin. When you run the standalone node REPL from the command prompt, what it's doing in the background is running repl.start() to give you the standard REPL.

However, the repl is pretty flexible. Here's an example that shows this off:

#!/usr/bin/env node

const net = require('net');
const repl = require('repl');

const mood = function () {
  const m = ['^__^', '-___-;', '>.<', '<_>'];
  return m[Math.floor(Math.random() * m.length)];

// A remote node repl that you can telnet to!
  .createServer(function (socket) {
    const remote = repl.start('node::remote> ', socket);
    // Adding "mood" and "bonus" to the remote REPL's context.
    remote.context.mood = mood;
    remote.context.bonus = 'UNLOCKED';

console.log('Remote REPL started on port 5001.');

// A "local" node repl with a custom prompt
const local = repl.start('node::local> ');

// Exposing the function "mood" to the local REPL's context.
local.context.mood = mood;

This script creates two REPLs: One is normal excepting for its custom prompt, but the other is exposed via the net module so you can telnet to it! In addition, it uses the context property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other doesn't really work.

In addition, all objects in the global scope will also be accessible to your REPLs.

Here's what happens when you run the script:

$ node repl.js
Remote REPL started on port 5001.
node::local> .exit
# <ctrl>-C

$ node repl.js
Remote REPL started on port 5001.
node::local> mood()
node::local> bonus
ReferenceError: bonus is not defined

As may be seen, the mood function is usable within the local REPL, but the bonus string is not. This is as expected.

Now, here's what happens when you try to telnet to port 5001:

$ telnet localhost 5001
Trying ::1...
Connected to localhost.
Escape character is '^]'.
node::remote> mood()
node::remote> bonus

As you can see, the mood function is also available over telnet! In addition, so is "bonus".

As an interesting consequence of my actions, bonus is now also defined on the local REPL:

node::local> bonus

It seems we "unlocked" the bonus string on the local REPL as well. As it turns out, any variables created in one REPL are also available to the other:

node::local> var node = "AWESOME!"

node::remote> node

As you can see, the node REPL is powerful and flexible.

Scroll to top