{"id":11610,"library":"qrcode-generator","title":"QR Code Generator for JavaScript","description":"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.","status":"active","version":"2.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/kazuhikoarase/qrcode-generator","tags":["javascript","qr","qrcode","generator","typescript"],"install":[{"cmd":"npm install qrcode-generator","lang":"bash","label":"npm"},{"cmd":"yarn add qrcode-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add qrcode-generator","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `qrcode` factory function is exported as the default export. Use this in modern module environments (ESM).","wrong":"import { qrcode } from 'qrcode-generator';","symbol":"qrcode","correct":"import qrcode from 'qrcode-generator';"},{"note":"For CommonJS environments, the `qrcode` factory function is the direct export of the module.","wrong":"const { qrcode } = require('qrcode-generator');","symbol":"qrcode","correct":"const qrcode = require('qrcode-generator');"},{"note":"Import the `QRCode` interface for type declarations when working with TypeScript.","symbol":"QRCode","correct":"import type { QRCode } from 'qrcode-generator';"}],"quickstart":{"code":"import qrcode from 'qrcode-generator';\n\n// Configuration for QR Code\nconst typeNumber = 4; // Type number (1-40), or 0 for auto-detection\nconst errorCorrectionLevel = 'M'; // Error correction level ('L', 'M', 'Q', 'H')\nconst dataToEncode = 'Hello, Checklist Day! This is a test QR code generated using qrcode-generator in a modern JavaScript environment.';\n\n// Create a QR Code instance\nconst qr = qrcode(typeNumber, errorCorrectionLevel);\n\n// Add data to the QR Code\nqr.addData(dataToEncode);\n\n// Make the QR Code (generate the matrix)\nqr.make();\n\n// Render the QR Code to an SVG string (Node.js example)\nconst svgString = qr.createSvgTag(5, 5); // cellSize = 5, margin = 5\n\nconsole.log('Generated SVG for QR Code:\\n', svgString);\n\n// Example for browser environment (hypothetical DOM interaction)\n// If running in a browser, you might append this to a DOM element:\n// document.getElementById('qr-container').innerHTML = qr.createImgTag(4, 4, 'My QR Code');","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always call `qr.make();` after `qr.addData(...)` and before any rendering or module inspection methods.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"Custom 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"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).","error":"ReferenceError: qrcode is not defined"},{"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.","cause":"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()`.","error":"QR code appears empty or contains incorrect data."},{"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.","cause":"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).","error":"TypeError: Cannot read properties of undefined (reading 'createSvgTag')"}],"ecosystem":"npm"}