npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ansi-to-web-style

v1.2.0

Published

convert ANSI color codes log to web console supported format specifier

Readme

ANSI to Console Style Converter

This tool converts strings containing ANSI escape sequences into format specifiers supported by Chrome DevTools Console, especially for old browsers or environments that don't support ANSI escape sequences like console-feed.

🚀 Quick Start

import {
  ansiToConsole,
  consoleLogAnsi,
  consoleLogAnsiParams,
  stripAnsi,
} from './ansiToConsole'

// Output colored text directly in the console (supports multiple arguments)
consoleLogAnsi('\x1b[31mError: \x1b[0mSomething went wrong!')

// Use multiple arguments, just like console.log
consoleLogAnsi('\x1b[31mError:\x1b[0m', 'Database connection failed:', {
  host: 'localhost',
  port: 5432,
})

// Get formatted parameter array (for custom handling)
const params = consoleLogAnsiParams(
  '\x1b[32mSuccess!\x1b[0m',
  'Task completed in',
  1.23,
  'seconds'
)
console.log(...params)

// Or get the conversion result for custom handling
const result = ansiToConsole('\x1b[32mSuccess!\x1b[0m')
console.log(result.text, ...result.styles)

📋 API Reference

ansiToConsole(ansiString: string): ConsoleResult

Converts an ANSI string to a Console style format.

Parameters:

  • ansiString: String containing ANSI escape sequences

Returns:

interface ConsoleResult {
  text: string // Formatted text with %c placeholders
  styles: string[] // Corresponding CSS style array
}

Example:

const result = ansiToConsole('\x1b[31mError:\x1b[0m File not found')
console.log(result.text, ...result.styles)
// result = {text: "%cError:%c File not found", styles: ["color: #e74c3c"]}
// Output: "%cError:%c File not found", "color: #e74c3c", ""

consoleLogAnsi(...args: any[]): void

Convenience function to output ANSI formatted text directly in the console. Supports multiple arguments, just like console.log. Automatically adds space separators between string arguments.

Parameters:

  • ...args: Strings containing ANSI escape sequences or any other arguments

Example:

// Single ANSI string
consoleLogAnsi('\x1b[32m✓ Task completed successfully!\x1b[0m')

// Multiple arguments (spaces are added automatically)
consoleLogAnsi(
  '\x1b[31mError:\x1b[0m',
  'Connection failed to',
  'localhost:5432'
)

// Mixed argument types
consoleLogAnsi(
  '\x1b[36mUser:\x1b[0m',
  {id: 123, name: 'John'},
  '\x1b[32m[Active]\x1b[0m'
)

consoleLogAnsiParams(ansiString: string, ...params: any[]): any[]

Convenience function to convert ANSI strings and parameters into a formatted parameter array.

Parameters:

  • ansiString: String containing ANSI escape sequences
  • ...params: Parameters to format

Returns:

  • Formatted parameter array

Example:

const params = consoleLogAnsiParams(
  '\x1b[32mSuccess!\x1b[0m',
  'Task completed in',
  1.23,
  'seconds'
)
// params = ["%cSuccess!", "color: #2ecc71", "Task completed in", 1.23, "seconds"]
console.log(...params)

stripAnsi(ansiString: string): string

Removes all ANSI escape sequences from a string and returns plain text.

Parameters:

  • ansiString: String containing ANSI escape sequences

Returns:

  • Cleaned plain text string

Example:

const cleaned = stripAnsi('\x1b[31mError:\x1b[0m Something went wrong')
console.log(cleaned) // "Error: Something went wrong"

🎯 Chrome Formatting Mechanism & Solution

Chrome Limitation

Chrome DevTools console.log has an important limitation: only the first argument is parsed as a formatted string for %c placeholders.

// ❌ Incorrect usage - the second '%cWorld' will not be parsed as a formatted string
console.log('%cHello', '%cWorld', 123, {foo: 'bar'}, '', 'color: red')

// ✅ Correct usage - merge all formatted content into the first argument
console.log('%cHello%cWorld', 'color: red', 'color: green', 123, {foo: 'bar'})

Our Solution

The consoleLogAnsi and consoleLogAnsiParams functions will automatically:

  1. Merge ANSI strings: Combine all strings containing ANSI sequences into a single formatted string
  2. Add space separators: Automatically add spaces between multiple strings, preserving the default behavior of console.log
  3. Style reset: Add empty styles for spaces to prevent style inheritance
  4. Preserve argument order: Non-string arguments retain their original position and type

Example Comparison

// Manual handling (tedious and error-prone)
console.log(
  '%cError:%c %cWarning',
  'color: red; font-weight: bold',
  '',
  'color: yellow',
  123,
  {foo: 'bar'}
)

// Using our function (simple and intuitive)
consoleLogAnsi('\x1b[31;1mError:\x1b[0m', '\x1b[33mWarning\x1b[0m', 123, {
  foo: 'bar',
})

Both produce the same effect, but the latter is easier to use and maintain.

🎨 Supported ANSI Codes

Color Codes

Foreground (Text Color)

  • \x1b[30m - Black
  • \x1b[31m - Red
  • \x1b[32m - Green
  • \x1b[33m - Yellow
  • \x1b[34m - Blue
  • \x1b[35m - Magenta
  • \x1b[36m - Cyan
  • \x1b[37m - White

Bright Foreground

  • \x1b[90m - Bright Black (Gray)
  • \x1b[91m - Bright Red
  • \x1b[92m - Bright Green
  • \x1b[93m - Bright Yellow
  • \x1b[94m - Bright Blue
  • \x1b[95m - Bright Magenta
  • \x1b[96m - Bright Cyan
  • \x1b[97m - Bright White

Background

  • \x1b[40m - Black background
  • \x1b[41m - Red background
  • \x1b[42m - Green background
  • \x1b[43m - Yellow background
  • \x1b[44m - Blue background
  • \x1b[45m - Magenta background
  • \x1b[46m - Cyan background
  • \x1b[47m - White background

Bright Background

  • \x1b[100m - Bright Black background
  • \x1b[101m - Bright Red background
  • \x1b[102m - Bright Green background
  • \x1b[103m - Bright Yellow background
  • \x1b[104m - Bright Blue background
  • \x1b[105m - Bright Magenta background
  • \x1b[106m - Bright Cyan background
  • \x1b[107m - Bright White background

Format Codes

  • \x1b[1m - Bold
  • \x1b[2m - Dim
  • \x1b[3m - Italic
  • \x1b[4m - Underline
  • \x1b[7m - Inverse
  • \x1b[9m - Strikethrough

Reset Codes

  • \x1b[0m - Reset all styles
  • \x1b[22m - Reset bold/dim
  • \x1b[23m - Reset italic
  • \x1b[24m - Reset underline
  • \x1b[27m - Reset inverse
  • \x1b[29m - Reset strikethrough
  • \x1b[39m - Reset foreground color
  • \x1b[49m - Reset background color

Example

Let console-feed support ANSI escape sequences

import React from 'react'
import {Hook, Console, Decode} from 'console-feed'
import {consoleLogAnsiParams} from 'ansi-to-web-style'

class App extends React.Component {
  state = {
    logs: [],
  }

  componentDidMount() {
    Hook(window.console, log => {
      const decodedLog = Decode(log)
      // @ts-ignore
      decodedLog.data = consoleLogAnsiParams(...log.data)
      // @ts-ignore
      this.setState(({logs}) => ({logs: [...logs, decodedLog]}))
    })

    console.log(`Hello world!`)
  }

  render() {
    return (
      <div style={{backgroundColor: '#242424'}}>
        <Console logs={this.state.logs} variant="dark" />
      </div>
    )
  }
}