Registry / serialization / qrcode-generator

qrcode-generator

JSON →
library2.0.4jsnpmunverified

qrcode-generator is a robust, pure JavaScript implementation for generating QR codes, supporting a wide range of features including various error correction levels (L, M, Q, H), different data encoding modes (Numeric, Alphanumeric, Byte, Kanji), and output formats (HTML Image Tag, SVG, Canvas, ASCII, Data URL). It is based on the JIS X 0510:1999 standard. The current stable version is 2.0.4, released in April 2024, with releases occurring periodically to address issues and add features like module support and TypeScript types in recent major versions. Key differentiators include its lightweight nature, direct HTML rendering capabilities, and foundational adherence to the QR Code specification, making it suitable for both browser and Node.js environments without external dependencies.

npm install qrcode-generator
INSTALL
IMPORT
SIG · QRCODE-GENERATOR
Q
qrcode-generator
serializationjavascriptv2.0.4
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.

qrcode
import qrcode from 'qrcode-generator';
import { qrcode } from 'qrcode-generator';
The primary `qrcode` factory function is exported as the default export. Use this in modern module environments (ESM).
qrcode
const qrcode = require('qrcode-generator');
const { qrcode } = require('qrcode-generator');
For CommonJS environments, the `qrcode` factory function is the direct export of the module.
QRCode
import type { QRCode } from 'qrcode-generator';
Import the `QRCode` interface for type declarations when working with TypeScript.

This example demonstrates how to import the `qrcode` factory function, configure a QR code, add data, generate the code matrix, and then render it as an SVG string, suitable for both Node.js console output or browser DOM manipulation.

import qrcode from 'qrcode-generator'; // Configuration for QR Code const typeNumber = 4; // Type number (1-40), or 0 for auto-detection const errorCorrectionLevel = 'M'; // Error correction level ('L', 'M', 'Q', 'H') const dataToEncode = 'Hello, Checklist Day! This is a test QR code generated using qrcode-generator in a modern JavaScript environment.'; // Create a QR Code instance const qr = qrcode(typeNumber, errorCorrectionLevel); // Add data to the QR Code qr.addData(dataToEncode); // Make the QR Code (generate the matrix) qr.make(); // Render the QR Code to an SVG string (Node.js example) const svgString = qr.createSvgTag(5, 5); // cellSize = 5, margin = 5 console.log('Generated SVG for QR Code:\n', svgString); // Example for browser environment (hypothetical DOM interaction) // If running in a browser, you might append this to a DOM element: // document.getElementById('qr-container').innerHTML = qr.createImgTag(4, 4, 'My QR Code');
Debug
Known issues
breakingVersion 2.0.0 introduced full module support, changing how the library is imported. Direct global access to `qrcode` (e.g., in a browser without an explicit import) may no longer work as expected without proper bundling or script loading, and `require()` patterns might need adjustment for the default export.
fix
For ES Modules, use `import qrcode from 'qrcode-generator';`. For CommonJS, use `const qrcode = require('qrcode-generator');`. Ensure proper script inclusion or bundling in browser environments if not using module imports.
affects: >=2.0.0
gotchaThe `make()` method *must* be called after adding data (`addData()`) and before attempting to render the QR code or query its modules (e.g., `createImgTag()`, `isDark()`, `getModuleCount()`). Failing to call `make()` will result in empty, incorrect, or incomplete QR code output.
fix
Always call `qr.make();` after `qr.addData(...)` and before any rendering or module inspection methods.
affects: >=1.0.0
gotchaCustom character set encoding for multibyte characters requires overwriting the internal `qrcode.stringToBytes` function. If not correctly implemented, non-ASCII characters might be encoded incorrectly or lead to issues.
fix
For non-default encoding, provide a custom implementation for `qrcode.stringToBytes(s)` that returns an array of byte numbers for the given string `s`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: qrcode is not defined
The `qrcode` factory function was accessed without being correctly imported or loaded into the execution context (e.g., forgetting an `import` statement in Node.js or a `<script>` tag in a browser).
fix
In module environments, add `import qrcode from 'qrcode-generator';` (ESM) or `const qrcode = require('qrcode-generator');` (CJS). In browsers, ensure the `qrcode.js` script is loaded before use.
QR code appears empty or contains incorrect data.
This often occurs when `qr.addData()` was not called with the desired content, or more commonly, when `qr.make()` was not invoked after adding data and before calling a rendering method like `createImgTag()` or `createSvgTag()`.
fix
Verify that `qr.addData('your content here')` is called with the correct string, and critically, that `qr.make();` is executed immediately after `addData()` and before any rendering functions.
TypeError: Cannot read properties of undefined (reading 'createSvgTag')
This error typically means the `qr` object itself is undefined, implying the `qrcode()` factory function call failed or returned an undefined value. This could be due to incorrect arguments or a fundamental issue with the `qrcode` function itself (though rare).
fix
Double-check the arguments passed to `qrcode(typeNumber, errorCorrectionLevel)` to ensure they are valid numbers and strings, respectively. Ensure `qrcode` itself is correctly imported and available.
Upgrade
Version history
2.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources