Generating QR Codes in Node.js: the qrcode Library and Examples

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 in Node.js with the qrcode Library



QR codes have become an integral part of the modern web: from Wi-Fi links to payment confirmations. If you are developing a backend on Node.js and want to programmatically create QR codes, the qrcode library is the most popular and simple tool. It does not require installing additional graphics packages and can output results in various formats: PNG, SVG, HTML canvas, and even a text view for the terminal.



Installation

Install the package via npm:

npm install qrcode


After installation, include the module in your project:

const QRCode = require('qrcode');


Main Methods

The library provides three main methods for generation:

  • QRCode.toDataURL(text, options) — returns a Data URL (base64 string with a PNG image).
  • QRCode.toBuffer(text, options) — returns a Buffer with a PNG image (convenient for saving to a file or sending via HTTP).
  • QRCode.toString(text, options) — returns a string as SVG or terminal characters (the output type is set in options.type).
  • QRCode.toCanvas(canvasElement, text, options) — for browser environments (canvas).


Simple Example: Creating a QR Code in PNG (Buffer)

The most common scenario is to generate an image and save it to a file or serve it to the user. Here is how to do it:

const QRCode = require('qrcode');const fs = require('fs');

QRCode.toBuffer('https://pythonlib.ru', { width: 400, margin: 2, color: { dark: '#000000', light: '#ffffff' }}).then(buffer => { fs.writeFileSync('qrcode.png', buffer); console.log('QR code saved to qrcode.png');}).catch(err => console.error(err));


In this example, we set the size to 400 pixels, a margin of 2 modules, and a black-and-white color scheme. The toBuffer method returns a Promise, so we use .then() or async/await.



Generating a QR Code in SVG

For web applications, it is often more convenient to use the vector SVG format — it scales without loss of quality and takes up less space. Specify type: 'svg' in the options:

const QRCode = require('qrcode');

QRCode.toString('https://pythonlib.ru', { type: 'svg', width: 300, color: { dark: '#1a73e8', light: '#ffffff' }}).then(svgString => { console.log(svgString); // Now you can save svgString to a file or insert it into HTML}).catch(err => console.error(err));


Outputting a QR Code Directly to the Terminal

Sometimes you need to quickly check a link without opening a browser. The library can draw a QR code using Unicode characters in the console:

const QRCode = require('qrcode');

QRCode.toString('Hello, PythonLib!', { type: 'terminal', small: true}).then(qr => { console.log(qr);}).catch(err => console.error(err));


The small: true parameter makes the QR code more compact (uses half blocks). If you remove it, the image will be larger but more detailed.



Settings and Parameters

The options object allows flexible control over generation. The main fields are:

  • width — the width of the image in pixels (default 256).
  • margin — the margin from the edge (in QR code modules, default 4).
  • color.dark — the color of dark modules (hex, rgb, or color name).
  • color.light — the background color (default white).
  • errorCorrectionLevel — the error correction level: 'L' (7%), 'M' (15%), 'Q' (25%), 'H' (30%). The higher the level, the more data can be recovered, but the QR code itself becomes denser.

Recommendations