You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Modern alternatives to the utf8 package for UTF-8 encoding and decoding
3
+
---
4
+
5
+
# Replacements for `utf8`
6
+
7
+
Modern Node and browsers provide native UTF-8 APIs, so this dependency is rarely needed.
8
+
9
+
## TextEncoder/TextDecoder (built-in)
10
+
11
+
The built-in [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) and [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) APIs provide a native way to handle UTF-8 encoding and decoding.
12
+
13
+
```ts
14
+
const text ="€";
15
+
const encoder =newTextEncoder();
16
+
const utf8Bytes =encoder.encode(text); // Uint8Array of UTF-8 bytes
Node's built-in [`Buffer`](https://nodejs.org/api/buffer.html) provides both `Buffer.from(str, 'utf8')` and `buf.toString('utf8')` methods for UTF-8 encoding and decoding.
27
+
28
+
```ts
29
+
const text ="€";
30
+
const utf8Buffer =Buffer.from(text, 'utf8'); // Buffer of UTF-8 bytes
0 commit comments