# string-width

> Get the visual width of a string - the number of columns required to display it

Latest version **8.2.2** (published 2026-07-08) · MIT license · 0 weekly downloads

> **Better alternative:** See documentation for alternatives (https://github.com/AikidoSec/module-replacements/blob/main/docs/string-width)

## Install

```sh
npm install string-width
pnpm add string-width
yarn add string-width
bun add string-width
```

## Health

**Score 70/100 (B)** — status: active.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 8.2.2 |
| Published | 2026-07-08 |
| First published | 2015-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 528 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | string, character, unicode, width, visual, column, columns, fullwidth, full-width, wcwidth, wcswidth, full, ansi, escape, codes, cli, command-line, terminal, console, cjk, chinese, japanese, korean, fixed-width, east-asian-width |

## Links

- npm: https://www.npmjs.com/package/string-width
- Repository: https://github.com/sindresorhus/string-width
- Homepage: https://github.com/sindresorhus/string-width#readme
- Issues: https://github.com/sindresorhus/string-width/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/string-width

## Dependencies (2)

- [strip-ansi](https://npm.io/package/strip-ansi.md) ^7.1.2
- [get-east-asian-width](https://npm.io/package/get-east-asian-width.md) ^1.5.0

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 8.2.2 (latest) — 2026-07-08
- 4.2.3 (version4) — 2021-09-23
- 8.2.1 — 2026-04-27
- 8.2.0 — 2026-02-18
- 8.1.1 — 2026-01-29
- 8.1.0 — 2025-09-01
- 8.0.0 — 2025-08-22
- 7.2.0 — 2024-06-30
- 7.1.0 — 2024-01-22
- 7.0.0 — 2023-10-28
- 6.1.0 — 2023-05-06
- 6.0.0 — 2023-05-06
- 5.1.2 — 2022-02-28
- 5.1.1 — 2022-02-27
- 5.1.0 — 2022-01-09
- … 15 more at https://npm.io/package/string-width/versions

## README

# string-width

> Get the visual width of a string - the number of columns required to display it

Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and do not affect the width.

Useful to be able to measure the actual width of command-line output.

## Install

```sh
npm install string-width
```

## Usage

```js
import stringWidth from 'string-width';

stringWidth('a');
//=> 1

stringWidth('古');
//=> 2

stringWidth('\u001B[1m古\u001B[22m');
//=> 2
```

## API

### stringWidth(string, options?)

#### string

Type: `string`

The string to be counted.

#### options

Type: `object`

##### ambiguousIsNarrow

Type: `boolean`\
Default: `true`

Count [ambiguous width characters](https://www.unicode.org/reports/tr11/#Ambiguous) as having narrow width (count of 1) instead of wide width (count of 2).

> Ambiguous characters behave like wide or narrow characters depending on the context (language tag, script identification, associated font, source of data, or explicit markup; all can provide the context). **If the context cannot be established reliably, they should be treated as narrow characters by default.**
> - http://www.unicode.org/reports/tr11/

##### countAnsiEscapeCodes

Type: `boolean`\
Default: `false`

Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted.

## Related

- [string-width-cli](https://github.com/sindresorhus/string-width-cli) - CLI for this module
- [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string
- [widest-line](https://github.com/sindresorhus/widest-line) - Get the visual width of the widest line in a string
- [get-east-asian-width](https://github.com/sindresorhus/get-east-asian-width) - Determine the East Asian Width of a Unicode character

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