QR code generator for both server-side and client-side applications with multiple output formats including PNG, SVG, Canvas, and Terminal rendering.
npx @tessl/cli install tessl/npm-qrcode@1.5.0QRCode is a comprehensive JavaScript library for generating QR codes that works seamlessly in both server-side Node.js environments and client-side browser applications. It offers multiple rendering options including canvas, SVG, PNG, and terminal output, supports all standard QR code encoding modes (Numeric, Alphanumeric, Kanji, and Byte), and provides advanced features like automatic mode optimization for minimal QR code size, mixed-mode encoding, error correction level selection, and multibyte character support including emojis.
npm install qrcodeNode.js (Server-side):
const QRCode = require('qrcode');
// Main entry point: ./lib/index.js -> ./lib/server.jsES6/TypeScript:
import QRCode from 'qrcode';Browser (Module bundlers like Webpack/Browserify):
const QRCode = require('qrcode');
// Browser entry point: ./lib/browser.js (configured in package.json)Browser (Precompiled bundle):
<script src="/node_modules/qrcode/build/qrcode.js"></script>
<!-- QRCode is now available globally -->const QRCode = require('qrcode');
// Generate QR code as Data URL (PNG)
QRCode.toDataURL('Hello World', function (err, url) {
if (err) throw err;
console.log(url); // data:image/png;base64,iVBORw0KGgoAAAA...
});
// Generate QR code as SVG string
QRCode.toString('Hello World', { type: 'svg' }, function (err, string) {
if (err) throw err;
console.log(string); // <svg xmlns="http://www.w3.org/2000/svg"...
});
// Using Promises (Node.js 8+)
try {
const url = await QRCode.toDataURL('Hello World');
const svg = await QRCode.toString('Hello World', { type: 'svg' });
} catch (err) {
console.error(err);
}QRCode is built around several key components:
Creates QR code symbols with automatic optimization and supports all standard encoding modes and error correction levels.
/**
* Creates QR Code symbol and returns a qrcode object
* @param {string|Array} text - Text to encode or array of segments
* @param {Object} options - Optional configuration
* @returns {Object} QRCode object with modules, version, errorCorrectionLevel, maskPattern, segments
*/
function create(text, options);Renders QR codes to HTML5 canvas elements and generates data URLs for browser applications.
/**
* Draws QR code to canvas or creates new canvas
* Overloaded function with multiple signatures
*/
function toCanvas(canvasElement, text, options, callback);
function toCanvas(text, options, callback);
/**
* Generates data URL containing QR code image
* Overloaded function with multiple signatures
*/
function toDataURL(text, options, callback);
function toDataURL(canvasElement, text, options, callback);Server-only functions for saving QR codes to files, generating buffers, and streaming output.
/**
* Saves QR Code to image file with automatic format detection
*/
function toFile(path, text, options, callback);
/**
* Generates QR code as Node.js Buffer
*/
function toBuffer(text, options, callback);
/**
* Writes QR Code to a writable stream (PNG only)
*/
function toFileStream(stream, text, options);Generates string representations of QR codes in multiple formats including UTF-8, SVG, and terminal output.
/**
* Returns a string representation of the QR Code
* @param {string|Array} text - Text to encode or segments
* @param {Object} options - String rendering options
* @param {Function} callback - Callback with (error, string)
* @returns {Promise<string>} String when using promises
*/
function toString(text, options, callback);Comprehensive configuration system for QR code generation parameters and rendering customization.
For advanced users who need precise control over QR code encoding:
const segments = [
{ data: 'HELLO', mode: 'alphanumeric' },
{ data: '123456', mode: 'numeric' }
];
QRCode.toDataURL(segments, function (err, url) {
console.log(url);
});Encode binary data using Uint8ClampedArray or Buffer:
const binaryData = [{ data: new Uint8ClampedArray([253, 254, 255]), mode: 'byte' }];
QRCode.toFile('binary.png', binaryData);Include optional SJIS conversion for Kanji mode:
const toSJIS = require('qrcode/helper/to-sjis');
QRCode.toDataURL('漢字', { toSJISFunc: toSJIS }, function (err, url) {
console.log(url);
});All functions support both callback and Promise patterns. Errors are thrown for:
The package includes a comprehensive command-line interface accessible after global installation:
npm install -g qrcode
# Generate QR code in terminal (UTF-8 output)
qrcode "Hello World"
# Save as PNG file with auto format detection
qrcode -o hello.png "Hello World"
# Save as SVG file
qrcode -o hello.svg "Hello World"
# Customize colors and options
qrcode -d FF0000 -l FFFFFF -o red.png "Hello World"
# Advanced options
qrcode -v 5 -e H -w 300 -o large.png "Important Data"
# Terminal output with custom options
qrcode --small --inverse "Compact Display"
# Read from stdin
echo "Dynamic Content" | qrcode -o dynamic.pngAvailable CLI Options:
-v, --qversion: QR Code version (1-40)-e, --error: Error correction level (L, M, Q, H)-m, --mask: Mask pattern (0-7)-t, --type: Output type (png, svg, utf8)-w, --width: Image width in pixels-s, --scale: Scale factor-q, --qzone: Quiet zone size-l, --lightcolor: Light RGBA hex color-d, --darkcolor: Dark RGBA hex color--small: Compact terminal output-i, --inverse: Invert colors-o, --output: Output file path