{"id":11236,"library":"lib0","title":"lib0 Isomorphic Utility Functions","description":"lib0 is a monorepo offering a comprehensive collection of isomorphic utility functions designed for both Node.js and browser environments. These utilities cover fundamental programming needs such as array manipulation, efficient binary encoding/decoding, cryptographic operations, assertions, and more. The package is currently at version `0.2.117` on npm, representing its stable release line. However, an active development branch is progressing towards `v1.0.0`, with frequent release candidates (e.g., `v1.0.0-rc.12`) introducing significant architectural changes, particularly within its `delta` and `schema` modules. It supports both CommonJS and ESM module formats. Its primary differentiator lies in providing a robust, performance-optimized, and isomorphic toolkit, minimizing the need for environment-specific code paths and enabling effective dead code elimination by bundlers. The project appears to have an active development cadence, pushing out release candidates regularly.","status":"active","version":"0.2.117","language":"javascript","source_language":"en","source_url":"https://github.com/dmonad/lib0","tags":["javascript","typescript"],"install":[{"cmd":"npm install lib0","lang":"bash","label":"npm"},{"cmd":"yarn add lib0","lang":"bash","label":"yarn"},{"cmd":"pnpm add lib0","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For specific utilities like `assert`, directly import the named export. Importing the entire namespace for simple functions can be less optimized if your bundler doesn't fully support dead code elimination.","wrong":"import * as error from 'lib0/error'; error.assert(...)","symbol":"assert","correct":"import { assert } from 'lib0/error'"},{"note":"lib0 provides both ESM and CommonJS exports. For modern Node.js (`>=16`) and browser environments, ESM imports are preferred. The `createEncoder` and `writeVarUint` functions are part of lib0's efficient binary encoding capabilities.","wrong":"const { createEncoder } = require('lib0/encoding')","symbol":"createEncoder, writeVarUint","correct":"import { createEncoder, writeVarUint } from 'lib0/encoding'"},{"note":"While aliasing `from` to `arrayFrom` can prevent name collisions, it's generally unnecessary for subpath imports in lib0 unless you have other `from` functions in the same scope. This utility converts array-like objects to true arrays.","wrong":"import { from as arrayFrom } from 'lib0/array'","symbol":"from","correct":"import { from } from 'lib0/array'"}],"quickstart":{"code":"import { assert } from 'lib0/error';\nimport { createEncoder, writeVarUint, writeString } from 'lib0/encoding';\nimport { createDecoder, readVarUint, readString } from 'lib0/decoding';\n\n// Basic assertion\nassert(1 + 1 === 2, 'Arithmetic should work!');\nconsole.log('Assertion passed: 1 + 1 === 2');\n\n// Encoding and decoding a simple value\nconst encoder = createEncoder();\nwriteVarUint(encoder, 12345);\nwriteString(encoder, 'Hello, lib0!');\nconst encodedData = encoder.buf;\n\nconsole.log(`Encoded data (Uint8Array): ${encodedData}`);\n\nconst decoder = createDecoder(encodedData);\nconst decodedNumber = readVarUint(decoder);\nconst decodedString = readString(decoder);\n\nconsole.log(`Decoded number: ${decodedNumber}`);\nconsole.log(`Decoded string: ${decodedString}`);\n\nassert(decodedNumber === 12345, 'Decoded number mismatch');\nassert(decodedString === 'Hello, lib0!', 'Decoded string mismatch');\nconsole.log('Encoding and decoding successful!');\n\n// Using an array utility\nimport { from } from 'lib0/array';\nconst iterable = new Set([1, 2, 3]);\nconst arr = from(iterable);\nassert(arr.length === 3 && arr[0] === 1, 'Array conversion failed');\nconsole.log(`Converted iterable to array: ${arr}`);","lang":"typescript","description":"Demonstrates basic usage of `lib0/error` for assertions, `lib0/encoding` and `lib0/decoding` for binary data manipulation, and `lib0/array` for common array utilities."},"warnings":[{"fix":"Consult the `v1.0.0` changelog and specific module documentation for migration guides. Extensive refactoring may be required for `delta` and `schema` module consumers.","message":"The upcoming `v1.0.0` major version introduces significant breaking changes, particularly within the `delta` and `schema` modules. Users relying on these specific functionalities in `0.x` versions will need to adapt their code when upgrading to `1.0.0` or later release candidates.","severity":"breaking","affected_versions":">=1.0.0-rc.0"},{"fix":"Upgrade your Node.js environment to version 16 or newer to ensure compatibility. This is an explicit engine requirement in the package metadata.","message":"lib0 requires Node.js version `16` or higher. Projects running on older Node.js runtimes will encounter compatibility issues, including syntax errors or missing global APIs.","severity":"breaking","affected_versions":"<=0.2.x"},{"fix":"Ensure your project's `package.json` `type` field and bundler configuration (e.g., Webpack, Rollup) are correctly set up for dual CJS/ESM compatibility, especially when consuming subpath imports. Prefer `import` statements over `require` in modern TypeScript/ESM projects.","message":"While lib0 ships with both CommonJS and ESM module formats, misconfigured bundlers or incorrect `package.json` configurations in consumer projects can lead to module resolution errors (e.g., `ERR_REQUIRE_ESM`) when attempting to `require()` an ESM-only entrypoint or vice-versa.","severity":"gotcha","affected_versions":">=0.2.x"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { someFunc } = require('lib0/module');` to `import { someFunc } from 'lib0/module';`. Ensure your project's `tsconfig.json` (if using TypeScript) and bundler are configured for ESM output where appropriate.","cause":"Attempting to use `require()` to import a `lib0` subpath module in an environment or bundler setup that expects ESM, or when `lib0` itself resolves to its ESM entrypoint for that subpath.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. Instead change the require of ... to a dynamic import() or an ESM import statement."},{"fix":"Verify your bundler (e.g., Webpack, Rollup, esbuild) configuration has `esModuleInterop: true` in TypeScript, and that tree-shaking rules are not overly aggressive or misconfigured for `lib0`'s subpath exports. Ensure direct named imports (`import { assert } from 'lib0/error'`) are used rather than default imports or namespace imports if only specific functions are needed.","cause":"This error often occurs due to issues with how bundlers or transpilers handle named exports from subpath imports, sometimes related to tree-shaking configurations or incorrect module interop settings.","error":"TypeError: (0 , _lib0_error.assert) is not a function"}],"ecosystem":"npm"}