QR Code Reader
qrcode-reader is a JavaScript library for decoding QR codes from various image data sources. It functions as a maintained fork of Lazarsoft's popular jsqrcode project, providing QR code reading capabilities in both Node.js environments and HTML5-enabled browsers. The package is currently at version 1.0.4, and while a formal release cadence isn't specified, its "maintained fork" status indicates ongoing support and bug fixes, though updates may not be frequent. A key characteristic is its pure JavaScript implementation, avoiding native dependencies that can complicate installation, particularly for cross-platform deployment. For Node.js usage, it necessitates an external image parsing library such as Jimp or image-parser to handle raw image buffers and convert them into the specific bitmap format expected by the decoder. Conversely, in browser contexts, it integrates directly with the native Image global object or canvas ImageData objects, simplifying client-side implementation. It utilizes a callback-based API for asynchronous operations.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'width')
cause The `qr.decode()` method was called with an incorrect or incomplete image bitmap object (e.g., missing `width`, `height`, or `data` properties), typically when `jimp` or `image-parser` did not properly process the image data.fixEnsure the external image parser (like Jimp) correctly loads the image and provides the bitmap data in the expected `{width, height, data}` format. Double-check the image path/buffer for corruption or invalid format. -
Error: ENOENT: no such file or directory, open './qrcode.png'
cause The image file specified could not be found or accessed by the Node.js process at the given path.fixVerify the absolute or relative path to the image file. Ensure the Node.js process has read permissions for the file, and that the file actually exists at the specified location.
Warnings
- gotcha In Node.js environments, `qrcode-reader` requires a separate image processing library (e.g., `jimp` or `image-parser`) to convert image files or buffers into a compatible bitmap format before decoding. The library itself does not handle raw image file parsing.
- gotcha The API is entirely callback-based, using a `qr.callback = function(error, result) { ... }` pattern. It does not return Promises, which is a common pattern in modern asynchronous JavaScript.
- gotcha Using `image-parser` as an alternative to `jimp` in Node.js introduces native dependencies (`lwip` or `graphicsmagick`), which can lead to complex installation issues, especially across different operating systems or environments.
Install
-
npm install qrcode-reader -
yarn add qrcode-reader -
pnpm add qrcode-reader
Imports
- QrCode
import QrCode from 'qrcode-reader';
const QrCode = require('qrcode-reader'); - QrCode (Browser Global)
<script src="dist/index.js"></script> // Then access via global QrCode variable
Quickstart
const fs = require('fs');
const Jimp = require('jimp');
const QrCode = require('qrcode-reader');
// IMPORTANT: For this code to run, you need a QR code image file (e.g., 'qrcode.png')
// in the same directory as your script, or update 'imagePath' to point to one.
// You can generate a simple 'Hello World' QR code using an online generator or a library.
const imagePath = './qrcode.png';
try {
const buffer = fs.readFileSync(imagePath);
Jimp.read(buffer, function(err, image) {
if (err) {
console.error("Error reading image with Jimp:", err);
return;
}
const qr = new QrCode();
qr.callback = function(err, value) {
if (err) {
console.error("QR Code decoding error:", err);
return;
}
console.log("QR Code result:", value.result);
console.log("Decoded value details:", value);
};
// The 'image.bitmap' object from Jimp provides { width, height, data }
// in a format expected by qrcode-reader.
qr.decode(image.bitmap);
});
} catch (readErr) {
console.error(`Failed to read QR code image at ${imagePath}. Please ensure the file exists and is accessible.\nIf you don't have one, create a simple 'qrcode.png' in the same directory as this script.`);
}