or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

browser-rendering.mdconfiguration.mdcore-generation.mdindex.mdserver-operations.mdstring-rendering.md
tile.json

tessl/npm-qrcode

QR code generator for both server-side and client-side applications with multiple output formats including PNG, SVG, Canvas, and Terminal rendering.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/qrcode@1.5.x

To install, run

npx @tessl/cli install tessl/npm-qrcode@1.5.0

index.mddocs/

QRCode

QRCode 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.

Package Information

  • Package Name: qrcode
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install qrcode

Core Imports

Node.js (Server-side):

const QRCode = require('qrcode');
// Main entry point: ./lib/index.js -> ./lib/server.js

ES6/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 -->

Basic Usage

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);
}

Architecture

QRCode is built around several key components:

  • Core Engine: QR code symbol generation with automatic mode selection and error correction
  • Rendering System: Multiple output renderers (PNG, SVG, Canvas, Terminal, UTF-8) with consistent options
  • Platform Adapters: Separate entry points for Node.js and browser environments
  • Data Encoding: Support for all QR code modes (Numeric, Alphanumeric, Byte, Kanji) with automatic optimization
  • Configuration System: Comprehensive options for QR code generation and rendering customization

Capabilities

Core QR Generation

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);

Core Generation

Browser Canvas Rendering

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);

Browser Rendering

Server-side File Operations

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);

Server Operations

String Output Rendering

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);

String Rendering

Configuration and Options

Comprehensive configuration system for QR code generation parameters and rendering customization.

Configuration

Advanced Features

Manual Segment Mode

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);
});

Binary Data Support

Encode binary data using Uint8ClampedArray or Buffer:

const binaryData = [{ data: new Uint8ClampedArray([253, 254, 255]), mode: 'byte' }];
QRCode.toFile('binary.png', binaryData);

Kanji Mode Support

Include optional SJIS conversion for Kanji mode:

const toSJIS = require('qrcode/helper/to-sjis');

QRCode.toDataURL('漢字', { toSJISFunc: toSJIS }, function (err, url) {
  console.log(url);
});

Error Handling

All functions support both callback and Promise patterns. Errors are thrown for:

  • Invalid input data
  • Data too large for specified QR code version
  • Invalid configuration options
  • File system errors (server-side functions)

CLI Interface

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.png

Available 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