CLI Spinners
cli-spinners is a JavaScript/TypeScript library that provides a comprehensive collection of over 70 distinct, pre-defined terminal spinners. Each spinner entry includes a recommended `interval` (in milliseconds) for frame display and an array of Unicode `frames` that constitute the animation sequence. The package's current stable version is 3.4.0, and it maintains an active release cadence, frequently adding new spinner designs. Its primary role is to serve as a data source for spinner animations, differentiating it from full-fledged terminal animation libraries like `ora`, which consume `cli-spinners`' data to render actual progress indicators. This design choice makes it highly flexible, as the underlying `spinners.json` file can be directly utilized in various environments and programming languages, with official ports existing for Python, Swift, Rust, Go, and Bash.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` `cli-spinners` in a CommonJS module after upgrading to v3.0.0.fixRefactor your code to use ES Modules (`import`) and ensure your project's `package.json` has `"type": "module"` or use `.mjs` file extensions. -
TypeError: cliSpinners is not a function
cause Trying to invoke `cliSpinners` as a function or accessing properties on an undefined `cliSpinners` variable due to incorrect import.fixEnsure you are using the correct ESM import syntax: `import cliSpinners from 'cli-spinners';` and accessing properties like `cliSpinners.dots` (it's an object, not a function). -
Cannot find module 'cli-spinners'
cause The package is not installed, or there is a typo in the import path/package name.fixRun `npm install cli-spinners` or `yarn add cli-spinners`. Double-check the spelling in your import statement.
Warnings
- breaking This package transitioned to pure ECMAScript Modules (ESM) in version 3.0.0. Attempting to use `require()` to import `cli-spinners` will result in an `ERR_REQUIRE_ESM` runtime error.
- breaking Version 3.0.0 of `cli-spinners` raised the minimum Node.js requirement to 18.x. Running this package on older Node.js versions will lead to runtime errors or compatibility issues.
- gotcha `cli-spinners` provides only the raw data (frames and recommended intervals) for spinners. It does not include any logic for animating or displaying these spinners in the terminal. If you need a fully functional terminal spinner, you should use a higher-level library like `ora`.
- gotcha Spinner frames use various Unicode characters. While most modern terminals and fonts support these, some older or non-standard terminal emulators might display characters incorrectly or as empty boxes, leading to broken animations.
Install
-
npm install cli-spinners -
yarn add cli-spinners -
pnpm add cli-spinners
Imports
- cliSpinners
const cliSpinners = require('cli-spinners');import cliSpinners from 'cli-spinners';
- randomSpinner
import cliSpinners, { randomSpinner } from 'cli-spinners';import { randomSpinner } from 'cli-spinners'; - Spinner
import type { Spinner } from 'cli-spinners';
Quickstart
import cliSpinners, { randomSpinner, type Spinner } from 'cli-spinners';
import process from 'node:process';
// Get a specific spinner by name
const dotsSpinner: Spinner = cliSpinners.dots;
console.log('Using "dots" spinner (first frame):', dotsSpinner.frames[0]);
// Get a random spinner programmatically
const randSpinner: Spinner = randomSpinner();
console.log(`\nUsing a random spinner (first frame): ${randSpinner.frames[0]}...`);
// Example of manually animating a spinner (ora is recommended for production use)
let frameIndex = 0;
const spinnerName = 'dots';
const spinner = cliSpinners[spinnerName as keyof typeof cliSpinners]; // Access dynamically with type assertion
const interval = spinner.interval;
const frames = spinner.frames;
process.stdout.write(`\nAnimating "${spinnerName}": `);
const timer = setInterval(() => {
process.stdout.write('\r'); // Clear the current line
process.stdout.write(`Animating "${spinnerName}": ${frames[frameIndex]}`);
frameIndex = (frameIndex + 1) % frames.length;
}, interval);
// Stop the animation after a few seconds
setTimeout(() => {
clearInterval(timer);
process.stdout.write('\r \r'); // Clear the spinner line completely
console.log(`\nStopped animating "${spinnerName}".`);
}, 3000);