restore-cursor

raw JSON →
5.1.0 verified Sat Apr 25 auth: no javascript

Gracefully restore the CLI cursor on exit. The package automatically restores the cursor when a process exits, preventing hidden cursors from remaining hidden after crashes or interruptions. Latest version 5.1.0, maintained by Sindre Sorhus. Releases are stable and infrequent. Key differentiators: zero dependencies (v5+), proper TTY detection (no-op in non-TTY contexts), and simple imperative API. Pure ESM from v4, requires Node.js >=18 (v5).

error Error [ERR_REQUIRE_ESM]: require() of ES Module
cause Using require() on a package that is pure ESM (v4+).
fix
Use dynamic import: const restoreCursor = (await import('restore-cursor')).default;
error SyntaxError: Unexpected token 'export'
cause Trying to import a package using ESM in a CommonJS environment without proper configuration.
fix
Set "type": "module" in package.json or use .mjs extension.
breaking This package is now pure ESM. It cannot be require()'d from CommonJS.
fix Use dynamic import() or switch to ES modules.
breaking Requires Node.js 18 or later.
fix Upgrade Node.js to >=18 or use v4.x (which requires Node.js 12.20).
breaking No longer prints control codes to non-terminal outputs.
fix If you rely on cursor restore in non-TTY contexts, you may need to handle that manually.
gotcha Calling restoreCursor() multiple times or when cursor was not hidden has no effect, but is safe.
fix No action needed; it is idempotent.
npm install restore-cursor
yarn add restore-cursor
pnpm add restore-cursor

Shows how to import and call restoreCursor to gracefully restore the cursor on exit. Demonstrates manual restore and automatic cleanup.

import restoreCursor from 'restore-cursor';

// Hide cursor (e.g., for a spinner)
process.stdout.write('\u001B[?25l');

// ... do work (if the process crashes, cursor is restored automatically)

// Restore cursor manually
restoreCursor();

// Or let the cleanup happen on exit