Skip to content

node:repl docs code not working (Starting multiple REPL instances against a single running instance) #3861

@martian17

Description

@martian17

Details

node:repl docs
Under Starting multiple REPL instances against a single running instance there is a code that demonstrates the sharing of a single global object among multiple repl instances. However, the code in question does not work. That is, when I run this code and connect to it with telnet localhost 5001, the variable that I define in the telnet instance does not get reflected in the normal stdin/out instance.
I found that this is most likely because useGlobal option is set to false by default in repl.start, as explained in the same document. To enable this, I needed to explicitly set this to true in repl.start argument like this. Now the code works as expected.

const net = require('node:net');
const repl = require('node:repl');
let connections = 0;

repl.start({
  prompt: 'Node.js via stdin> ',
  input: process.stdin,
  output: process.stdout,
  useGlobal: true   //modified
});

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via Unix socket> ',
    input: socket,
    output: socket,
    useGlobal: true //modified
  }).on('exit', () => {
    socket.end();
  });
}).listen('/tmp/node-repl-sock');

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via TCP socket> ',
    input: socket,
    output: socket,
    useGlobal: true //modified
  }).on('exit', () => {
    socket.end();
  });
}).listen(5001);

Also, there is a second but minor problem in this code. After I exit from this program and try to execute this again, the following error is thrown.

Node.js via stdin> node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use /tmp/node-repl-sock
    at Server.setupListenHandle [as _listen2] (node:net:1302:21)
    at listenInCluster (node:net:1367:12)
    at Server.listen (node:net:1465:5)
    at Object.<anonymous> (/Users/yoshi/prog/repl/telnet2.js:22:4)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1346:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '/tmp/node-repl-sock',
  port: -1
}

To get this to work again, I had to remove /tmp/node-repl-sock file from my laptop. I don't have a good idea on how Unix socket works, but I think there should be an additional cleanup process in the code.

Node.js version

v16.8.0

Example code

No response

Operating system

macOS Mojave 10.14.6

Scope

docs node:repl docs

Module and version

Not applicable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions