Skip to content

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

Description

@psqq

Node.js Version

v26.5.0

NPM Version

11.17.0

Operating System

Linux archlinux 7.0.8-arch1-1 nodejs/help#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

Activity

  1. avivkeller commented on Jul 14, 2026

    @avivkeller
    Member

    Maybe we can allow users to specify a history option with their own file path?

  2. transferred this issue fromnodejs/helpon Jul 14, 2026
  3. mhayk commented on Jul 15, 2026

    @mhayk
    Contributor

    I tried to reproduce this on both v26.5.0 (official binary) and the current main, and multiline entries are correctly persisted as a single physical line with \r separators (e.g. 2]\r[1,) when using repl.start() + setupHistory() — typed, pasted, and with history navigation.

    Multiline history support landed in 4a4aa58 and shipped in v25.0.0, and the behaviour you describe matches what older versions (≤ v24) produce. Could you double-check the actual binary being run (node --version from the same environment)?

    While investigating, I did find a related edge case: if lines are evaluated while setupHistory() is still loading the history file asynchronously, the in-memory entries are discarded when the file load completes, leaving the history file empty. I'll send a PR for that shortly.

  4. psqq commented on Jul 15, 2026

    @psqq
    Author

    @mhayk, thanks for the response. I was indeed mistaken, and I apologize for wasting your time.

    I confirm that in versions v24.14.1 and v26, the history works correctly with \r.

    I probably switched to version 22 and noticed the issue there. And when I was checking on 26, I opened the history file in VS Code, which doesn't display \r. After opening it in nvim, I saw that everything is fine.

    I will close this issue since it is no longer relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions