jpeg-js is a pure JavaScript library designed for encoding and decoding JPEG images without relying on native binaries or external dependencies. This makes it suitable for various JavaScript environments, including Node.js and browsers (browser support was explicitly added in v0.4.0). It offers synchronous APIs for converting raw image data to the JPEG format and vice-versa, providing direct control over image processing. The current stable version is 0.4.4, released in June 2022. The project has a relatively slow release cadence, with the most recent updates focusing on bug fixes and minor feature enhancements. Its primary differentiating factor is its complete JavaScript implementation, beneficial for environments with strict dependency constraints or where native modules are problematic. However, a significant trade-off is its performance: it is a CPU-blocking library and is considerably slower than native alternatives like `sharp` or browser-native `Canvas API`, which is a crucial consideration for any performance-sensitive application.
npm install jpeg-jsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates both decoding an existing JPEG file and encoding raw RGBA pixel data into a new JPEG file, highlighting common usage patterns and important options.
For performance-critical scenarios, consider using native solutions like `sharp` (Node.js) or the browser's `Canvas API` and Web Workers to offload processing from the main thread.
Upgrade to `jpeg-js@0.4.2` or higher. Always use `Buffer.from()`, `Buffer.alloc()`, or `Buffer.allocUnsafe()` for creating Buffers in your application code, as demonstrated in the quickstart.
Catch potential errors during `jpeg.decode`. Adjust `maxResolutionInMP` and `maxMemoryUsageInMB` in the decode options if you intend to process very large images, but be mindful of memory consumption.
Upgrade to `jpeg-js@0.4.4` or higher, which includes a fix to rethrow a more helpful error in these scenarios. For browser environments, ensure you have a `Buffer` polyfill if `useTArray: false` is desired, or explicitly use `useTArray: true` in decode options to get `Uint8Array`.
Increase the `maxResolutionInMP` option in `jpeg.decode()` if you expect to handle larger images, e.g., `{ maxResolutionInMP: 200 }`.Increase the `maxMemoryUsageInMB` option in `jpeg.decode()` if you have more memory available and expect to handle memory-intensive images, e.g., `{ maxMemoryUsageInMB: 1024 }`.If running in a browser, ensure you have a `Buffer` polyfill or set `useTArray: true` in the `jpeg.decode()` options to receive pixel data as `Uint8Array`. Upgrade to `jpeg-js@0.4.4` for improved error messaging.
Use the correct import syntax: `import * as jpeg from 'jpeg-js';` for ESM, then access `jpeg.decode()` or `jpeg.encode()`. For CommonJS, `const jpeg = require('jpeg-js');` is correct.No dependency data recorded yet.