Skip to content

How to programmatically save multiline REPL history with \r (like native Node CLI)? #5159

Description

@psqq

Node.js Version

v26.5.0

NPM Version

11.17.0

Operating System

Linux archlinux 7.0.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 15 May 2026 16:45:15 +0000 x86_64 GNU/Linux

Subsystem

repl

Description

Description

When using the native Node.js binary REPL via terminal (node), multiline inputs (like objects, functions, or arrays) are correctly saved into .node_repl_history as a single physical line, where internal line breaks are represented by \r.

However, when creating a REPL instance programmatically using repl.start() and initializing history via repl.setupHistory(), every press of the Enter key triggers a write to the history file immediately. This breaks multiline inputs into separate, independent physical lines separated by \n. This completely breaks history navigation inside the programmatic REPL (pressing the Arrow Up key scrolls lines one by one instead of expanding the whole block).

Minimal Reproducible Example

Here is a short script demonstrating the issue:

import repl from 'node:repl';
import path from 'node:path';

const historyFile = path.join(import.meta.dirname, 'history.txt');

const r = repl.start({ prompt: '> ', terminal: true });
r.setupHistory(historyFile, () => {});

If you run this script and type a multiline array:

> [1,
| 2]
[ 1, 2 ]
>

The resulting history.txt contains separate lines in reverse order:

2]
[1,

While the native node history file combines them into a single line using \r:

2]\r[1,

Question

Is there a clean, official way to make a programmatic REPL handle multiline history exactly like the native CLI?

Minimal Reproduction

No response

Output

No response

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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