# colors-option

> Let users toggle colors

Latest version **6.1.2** (published 2025-11-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install colors-option
pnpm add colors-option
yarn add colors-option
bun add colors-option
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.1.2 |
| Published | 2025-11-08 |
| First published | 2021-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18.18.0 |
| Dependencies | 2 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | ehmicky |
| Maintainers | ehmicky |
| Keywords | color, colors, option, config, configuration, disable, chalk, typescript, ansi, rgb, terminal, tty, shell, bash, cli, es6, javascript, nodejs, library, windows |

## Links

- npm: https://www.npmjs.com/package/colors-option
- Repository: https://github.com/ehmicky/colors-option
- Homepage: https://www.github.com/ehmicky/colors-option
- Issues: https://github.com/ehmicky/colors-option/issues
- npm.io page: https://npm.io/package/colors-option

## Dependencies (2)

- [chalk](https://npm.io/package/chalk.md) ^5.4.1
- [is-plain-obj](https://npm.io/package/is-plain-obj.md) ^4.1.0

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 6.1.2 (latest) — 2025-11-08
- 6.1.1 — 2025-11-06
- 6.1.0 — 2025-11-05
- 6.0.1 — 2025-03-29
- 6.0.0 — 2023-10-28
- 5.0.0 — 2023-05-13
- 4.5.0 — 2022-11-11
- 4.4.0 — 2022-10-14
- 4.3.1 — 2022-10-13
- 4.3.0 — 2022-10-13
- 4.2.0 — 2022-08-16
- 4.1.0 — 2022-07-18
- 4.0.0 — 2022-05-09
- 3.0.0 — 2021-11-26
- 2.0.1 — 2021-06-22
- … 2 more at https://npm.io/package/colors-option/versions

## README

[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js&colorA=404040&logoColor=66cc33)](https://www.npmjs.com/package/colors-option)
[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.d.ts)
[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov&colorA=404040)](https://codecov.io/gh/ehmicky/colors-option)
[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky)
[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium&colorA=404040)](https://medium.com/@ehmicky)

Let users toggle colors.

This is a thin wrapper around the popular
[`chalk`](https://github.com/chalk/chalk) colors library that adds support for:

- A [`colors` boolean option](#colors)
- The [`NO_COLOR`](https://no-color.org/) and
  [`NODE_DISABLE_COLORS`](https://nodejs.org/api/cli.html#cli_node_disable_colors_1)
  environment variables

# Example

```js
import colorsOption from 'colors-option'

const exampleLibrary = ({ colors, ...otherLibraryOptions }) => {
  const chalk = colorsOption({ colors })
  console.log(chalk.red('example'))
}
```

# Install

```
npm install colors-option
```

This package works in Node.js >=18.18.0.

This is an ES module. It must be loaded using
[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),
not `require()`. If TypeScript is used, it must be configured to
[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),
not CommonJS.

# API

## colorsOption(options?)

`options`: `object`\
_Return value_: [`Chalk` instance](https://github.com/chalk/chalk#api)

### options

#### colors

_Type_: `boolean`

Whether colors should be displayed or not.

The default value depends on whether the [`stream`](#stream) supports colors.
Therefore, this is only meant to override that.

Instead of using
[`chalk/supports-color`](https://github.com/chalk/supports-color), the default
value relies on Node.js built-in colors detection
[`getColorDepth()`](https://nodejs.org/api/tty.html#tty_writestream_getcolordepth_env)
which:

- Supports the [`NO_COLOR`](https://no-color.org/) and
  [`NODE_DISABLE_COLORS`](https://nodejs.org/api/cli.html#cli_node_disable_colors_1)
  environment variables.
- Does not guess colors detection based on the presence of a
  [`--colors` CLI flag](https://github.com/chalk/supports-color#info). This
  gives finer control and flexibility over how to expose this as a CLI flag.

#### level

_Type_: `1 | 2 | 3`

[How many colors](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) to
display. The value can be:

- `1`: 16 (4 bits)
- `2`: 256 (8 bits)
- `3`: 16 millions (24 bits)

The default value depends on
[how many colors](https://nodejs.org/api/tty.html#writestreamgetcolordepthenv)
the [`stream`](#stream) supports. Therefore, this is only meant to override
that.

#### stream

_Type_:
[`Stream`](https://nodejs.org/api/stream.html#stream_class_stream_writable)\
_Default_: [`process.stdout`](https://nodejs.org/api/process.html#process_process_stdout)

Stream used to detect the default value of the [`colors`](#colors) and
[`level`](#level) options.

This should be the file or terminal where the colors are output.

# See also

- [`chalk-string`](https://github.com/ehmicky/chalk-string): Chalk with style
  strings.
- [`terminal-theme`](https://github.com/ehmicky/terminal-theme): 🎨 Use a color
  theme for your code's terminal output.

# Support

For any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).

Everyone is welcome regardless of personal background. We enforce a
[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and
inclusive environment.

# Contributing

This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.

If the documentation is unclear or has a typo, please click on the page's `Edit`
button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our
[guidelines](CONTRIBUTING.md). Pull requests are welcome!

<!-- Thanks go to our wonderful contributors: -->

<!-- ALL-CONTRIBUTORS-LIST:START -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<!--
<table>
  <tr>
    <td align="center"><a href="https://fosstodon.org/@ehmicky"><img src="https://avatars2.githubusercontent.com/u/8136211?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ehmicky</b></sub></a><br /><a href="https://github.com/ehmicky/colors-option/commits?author=ehmicky" title="Code">💻</a> <a href="#design-ehmicky" title="Design">🎨</a> <a href="#ideas-ehmicky" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ehmicky/colors-option/commits?author=ehmicky" title="Documentation">📖</a></td>
  </tr>
</table>

-->
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

---
_Source: https://npm.io/package/colors-option · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
