JavaScript (Node.js) QR Code Generator with qrcode and qr-image

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating QR Codes on Node.js: qrcode and qr-image Libraries



QR codes have become an integral part of modern web applications: from website links and authentication to transferring contact information and payment data. In the Node.js ecosystem, there are several mature libraries that allow you to generate QR codes without third-party APIs or heavy graphic editors. In this article, we will take a detailed look at the two most popular ones: qrcode and qr-image. You will learn how to install each one, what output formats they support, and get ready-to-use code examples for integration into your project.



1. qrcode Library — Universal Generator



qrcode is a powerful library with support for JavaScript, TypeScript, and even browsers. It allows you to generate QR codes in PNG, SVG, canvas formats, and even as plain text. The main advantage is detailed customization: you can change colors, error correction level, add logos, and manage margins.



Installation

npm install qrcode


Basic Example: Creating a PNG Image

const QRCode = require('qrcode');

// Simple string to encodeconst data = 'https://pythonlib.ru';

QRCode.toFile('qrcode.png', data, { color: { dark: '#000000', // color of modules (squares) light: '#FFFFFF' // background color }, errorCorrectionLevel: 'H' // maximum error correction}, function (err) { if (err) throw err; console.log('QR code saved as qrcode.png');});


Generating SVG (Vector Format)

const QRCode = require('qrcode');

QRCode.toString('https://pythonlib.ru', { type: 'svg', color: { dark: '#2c3e50', light: '#ecf0f1' }}, function (err, svgString) { if (err) throw err; console.log(svgString); // output SVG markup to console});


Generating to Buffer (for Sending via HTTP)

const QRCode = require('qrcode');

QRCode.toBuffer('https://pythonlib.ru', { type: 'png', width: 400, margin: 2}, function (err, buffer) { if (err) throw err; // buffer is a Buffer object that can be sent as an HTTP response // res.setHeader('Content-Type', 'image/png'); // res.send(buffer);});


2. qr-image Library — Lightweight Alternative



qr-image is a minimalistic library with no extra dependencies. It is focused on fast generation of PNG, SVG, EPS, and even PDF (via installing an additional module). If you don't need complex color settings and logos, qr-image will be an excellent choice.



Installation

npm install qr-image


Example: Creating a PNG Image

const qr = require('qr-image');const fs = require('fs');

const data = 'https://pythonlib.ru';const qr_png = qr.image(data, { type: 'png', size: 10 });qr_png.pipe(fs.createWriteStream('qr-image.png'));

console.log('QR code saved as qr-image.png');


Generating SVG

const qr = require('qr-image');const fs = require('fs');

const data = 'https://pythonlib.ru';const qr_svg = qr.image(data, { type: 'svg', size: 10 });qr_svg.pipe(fs.createWriteStream('qr-image.svg'));

console.log('QR code saved as qr-image.svg');


Getting SVG String Without Saving to a File

const qr = require('qr-image');

const data = 'https://pythonlib.ru';const svgString = qr.imageSync(data, { type: 'svg' });console.log(svgString); // ready SVG markup


3. Library Comparison



Recommendations