Color Stringifier
color-stringify is a JavaScript utility for converting various color value representations (like arrays of RGB/HSL components or objects) into standard CSS color strings. It supports outputting in formats such as hex (`#RRGGBB`), keywords (`red`), percentage (`rgb(10%, 20%, 30%)`), and several Adobe-specific formats, alongside the default `rgba()` or `hsla()` output. The library is currently at version 1.2.1, with its last update in early 2019, suggesting an abandoned or maintenance-only release cadence. It differentiates itself by offering a specific utility for stringifying pre-parsed color values, often paired with a parsing library like `color-parse`.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... From `color-stringify`
cause `color-stringify` is a CommonJS module being imported into an ES module context without proper interoperability.fixIf in a modern Node.js ESM module (e.g., `"type": "module"` in `package.json`), you cannot directly `require()` a CJS module in all cases. Try `import * as stringify from 'color-stringify';` or consider a CJS-compatible wrapper, but the most reliable fix is often to use `require()` in a CJS context or migrate to an ESM-first alternative. For this specific package, revert to CJS usage if possible: `const stringify = require('color-stringify');`. -
TypeError: stringify is not a function
cause Incorrect import of the `stringify` function. If you used `import { stringify } from 'color-stringify';` in a CJS context, or if `require('color-stringify')` returned an object instead of the function itself.fixEnsure you are using `const stringify = require('color-stringify');` as the package exports the function directly as `module.exports`. If you are in an ESM context trying to import, see the `ERR_REQUIRE_ESM` warning.
Warnings
- breaking This package is CommonJS-only and does not provide native ES module exports. Direct `import` statements will fail in pure ESM environments or modern Node.js versions without specific configurations (e.g., `--experimental-json-modules` or transpilation).
- gotcha The package has not been updated since February 2019 (version 1.2.1). This indicates it is no longer actively maintained and may not be compatible with newer JavaScript features, environments, or security standards. There are no TypeScript definitions shipped with the package.
- gotcha When using `type: 'keyword'`, the input color must exactly match a known CSS color name's RGB value. If no exact match is found, it will fall back to a default `rgba()` representation, which might be unexpected.
Install
-
npm install color-stringify -
yarn add color-stringify -
pnpm add color-stringify
Imports
- stringify
import stringify from 'color-stringify';
const stringify = require('color-stringify'); - stringify
import { stringify } from 'color-stringify';const { stringify } = require('color-stringify');
Quickstart
const stringify = require('color-stringify');
// Example 1: HSL array to HSLA string
const hslaString = stringify([120, 100, 100, 0.4], 'hsl');
console.log(`HSL array to HSLA: ${hslaString}`);
// Expected: hsla(120, 100%, 100%, .4)
// Example 2: RGB array to default RGBA string
const rgbaString = stringify([255, 0, 128, 0.75]);
console.log(`RGB array to RGBA: ${rgbaString}`);
// Expected: rgba(255, 0, 128, 0.75)
// Example 3: RGB object (from color-parse) to HEX string
const colorObject = { r: 51, g: 153, b: 255, alpha: 1, type: 'rgb' };
const hexString = stringify(colorObject, 'hex');
console.log(`RGB object to HEX: ${hexString}`);
// Expected: #3399FF
// Example 4: RGB array to keyword (if available)
const keywordString = stringify([255, 0, 0], 'keyword');
console.log(`RGB array to keyword: ${keywordString}`);
// Expected: red