Registry / serialization / utf8
library3.0.0jsnpmunverified

The `utf8.js` package, currently at its stable version 3.0.0 (last updated in late 2017), provides a comprehensively tested and robust JavaScript implementation for encoding and decoding UTF-8 strings. It distinguishes itself by strictly adhering to the Encoding Standard, ensuring precise handling of all scalar Unicode code point values. A core aspect of its design is strict error handling: the library explicitly throws an `Error` when attempting to encode non-scalar values (such as lone surrogates) or when encountering malformed UTF-8 data during decoding. This approach prioritizes data integrity over silent error correction. For developers requiring the ability to encode or decode non-scalar values, the related `WTF-8` library is recommended. Given its foundational utility and mature status, the project is considered to be in maintenance mode, receiving updates primarily for critical issues rather than frequent feature additions.

npm install utf8
INSTALL
IMPORT
SIG · UTF8
U
utf8
serializationjavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

utf8
const utf8 = require('utf8');
import utf8 from 'utf8';
This package (v3.0.0) is primarily a CommonJS module. Direct ES module imports are not officially supported and will require a bundler or dynamic import for compatibility.
global utf8 object
<!-- In browser --> <script src="utf8.js"></script> <script> const encoded = utf8.encode('Hello'); </script>
When included directly in a browser via a <script> tag, the library exposes a global `utf8` object on the `window`.
utf8.encode
const utf8 = require('utf8'); const encodedString = utf8.encode('Hello, world! 😊');
const { encode } = require('utf8'); // Not a named export
The `encode` method is accessed as a property of the main `utf8` object.

This quickstart demonstrates how to encode and decode UTF-8 strings, including those with multi-byte Unicode characters, and how to check the library's version.

const utf8 = require('utf8'); // Example 1: Encoding a basic string const originalString1 = 'Hello, world! 👋'; const encodedString1 = utf8.encode(originalString1); console.log(`Original: '${originalString1}'`); console.log(`Encoded: '${encodedString1}'`); const decodedString1 = utf8.decode(encodedString1); console.log(`Decoded: '${decodedString1}'`); // Example 2: Encoding a string with a multi-byte Unicode character // U+1F60A SMILING FACE WITH SMILING EYES const originalString2 = 'Smiling face: \uD83D\uDE0A'; const encodedString2 = utf8.encode(originalString2); console.log(`\nOriginal: '${originalString2}'`); console.log(`Encoded: '${encodedString2}'`); const decodedString2 = utf8.decode(encodedString2); console.log(`Decoded: '${decodedString2}'`); // Example 3: Demonstrate version access console.log(`\nutf8.js version: ${utf8.version}`);
Debug
Known issues
gotchaThe `utf8.encode()` method will throw an `Error` if the input JavaScript string contains a non-scalar value, specifically a lone surrogate (a `U+D800` to `U+DFFF` code point not part of a valid surrogate pair). This strict behavior is by design, adhering to the Encoding Standard for proper UTF-8.
fix
Ensure input strings are 'well-formed' Unicode. Pre-process strings to remove or replace lone surrogates (e.g., with `U+FFFD` replacement character) before calling `utf8.encode()`. For scenarios requiring encoding of non-scalar values, consider using the `WTF-8` library.
affects: >=2.0.0
gotchaThe `utf8.decode()` method will throw an `Error` when it detects malformed UTF-8 byte sequences in the input `byteString`. This strictness prevents silent data corruption that can occur with lenient decoders.
fix
Verify the source of the input `byteString` to ensure it is correctly encoded as UTF-8. Implement robust error handling using `try...catch` blocks around `utf8.decode()` calls to gracefully manage potentially malformed input, or pre-validate the input if possible.
affects: >=2.0.0
Errors
Common errors & fixes
Error: A lone surrogate code point was found.
Attempting to encode a JavaScript string containing an unpaired (lone) surrogate character (e.g., `\uD800` without a trailing `\uDC00` to `\uDFFF`).
fix
Validate and sanitize input strings to ensure they are well-formed Unicode before passing them to `utf8.encode()`. You can replace lone surrogates or use `String.prototype.toWellFormed()` (if targeting environments that support it) or related libraries.
Error: Malformed UTF-8 data.
The input string provided to `utf8.decode()` contains byte sequences that do not conform to valid UTF-8 encoding rules.
fix
Check the origin and integrity of the byte string being decoded. Ensure it has been correctly encoded as UTF-8. Wrap `utf8.decode()` calls in a `try...catch` block to handle invalid input gracefully, e.g., by logging the error and using a fallback or replacement.
TypeError: require is not a function (in ES module context) or SyntaxError: Cannot use import statement outside a module (in CJS context)
This package is a CommonJS module. Using `require()` in an ES module context or `import` in a CommonJS context will lead to module resolution errors.
fix
For Node.js, ensure you use `const utf8 = require('utf8');` in CommonJS modules (`.js` files where `"type": "module"` is not set or in `.cjs` files). If working in an ES module environment (`.mjs` files or `"type": "module"` in `package.json`), you may need to use dynamic `import('utf8')` or rely on a bundler like Webpack or Rollup to handle the CommonJS dependency.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
utf8 — npm install utf8 · libregistry