sparkline
sparkline is a JavaScript library that generates textual sparklines (small, intense, word-sized graphics) inspired by Holman's 'spark' utility. It takes an array of numerical data and outputs a compact string representation of a line graph, primarily for console or plain text display. The current stable version, 0.2.0, was last released on June 23, 2017. Given the lack of updates since then, its development is considered abandoned. It differentiates itself by producing character-based sparklines suitable for terminal output or simple HTML, in contrast to modern SVG/Canvas-based solutions, and predates the common adoption of ES Modules (ESM) and TypeScript in the JavaScript ecosystem.
Common errors
-
TypeError: sparkline is not a function
cause Attempting to use `sparkline` as a named or default export in an ES Module context, or calling it without first requiring it in a CommonJS context.fixEnsure you are using the CommonJS `require` syntax in Node.js (`const sparkline = require('sparkline');`) or that the script is loaded globally in the browser context before calling `sparkline()`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... sparkline.js not supported.
cause A modern Node.js or bundler environment is configured to treat modules as ESM by default and does not correctly handle the CJS `sparkline` package, preventing it from being `require`d.fixThis error typically occurs when trying to `require` an ES Module from a CJS context. While `sparkline` is CJS, this error can arise if the surrounding project is incorrectly treating it as ESM. Ensure your project and bundler correctly identify `sparkline` as CJS. If `package.json` has `"type": "module"`, `require()` calls inside `.js` files need specific handling, or ensure the consumer file is `.cjs` or `require`d from a CJS file. For Node.js, ensure your consuming script is also CommonJS. For bundlers, check their specific configuration for CJS compatibility. -
Module not found: Error: Can't resolve 'sparkline'
cause The module 'sparkline' could not be found by the module resolver, often due to an incorrect import path, missing installation, or a bundler struggling with legacy CJS packages in a modern setup.fixFirst, ensure `npm install sparkline` has been run. If the issue persists in a modern bundler (e.g., Webpack, Rollup, Vite), it might be a compatibility issue with CJS. Check the bundler's configuration for `resolve` or `alias` settings to ensure it can locate and process CommonJS modules.
Warnings
- gotcha This package is not actively maintained, with the last release (v0.2.0) dating back to June 2017. It does not receive updates for bug fixes, performance improvements, or compatibility with newer JavaScript features or environments.
- gotcha The package is distributed as CommonJS (CJS) only, without native ES Module (ESM) support. Importing it directly in an ESM-only environment or bundler configuration may lead to import errors or require specific CJS compatibility settings.
- gotcha The library does not provide TypeScript type definitions. Integrating it into a TypeScript project will require creating custom declaration files (`.d.ts`) to avoid type errors and enable type checking.
Install
-
npm install sparkline -
yarn add sparkline -
pnpm add sparkline
Imports
- sparkline
import sparkline from 'sparkline';
const sparkline = require('sparkline'); - sparkline
import { sparkline } from 'sparkline';sparkline([1, 5, 22, 7, 9]);
Quickstart
const sparkline = require('sparkline');
// Generate a sparkline from an array of numbers
const data = [1, 5, 22, 7, 9, 15, 3, 18, 10, 25];
const line = sparkline(data);
console.log('Node.js Console Sparkline:', line);
// Example with more data points and options (inferred from v0.2.0 release notes)
// The README only shows basic usage, but 'html' option was added in v0.2.0
// For basic console output, the array is sufficient.
const largerData = Array.from({ length: 20 }, (_, i) => Math.sin(i / 2) * 10 + 10 + Math.random() * 5);
const anotherLine = sparkline(largerData);
console.log('Another Sparkline:', anotherLine);
// To run as a command-line utility:
// npm install -g sparkline
// sparkline 12 43 56 30 15
// echo 21,45,6,25,19,15 | sparkline