How to Generate QR Code Using Node.js
How to Generate QR Code Using Node.js

How to Generate QR Code Using Node.js

Spread the love
QR Code in Node.js
Qr code in Node.js

QR means “Quick Response”. It has become an important part of life. It can store a large amount of data. QR scanner can instantly process the data by scanning it. With the increase in digital devices, you can access any data by just scanning it. There are various use cases of QR Code like in sales & marketing, for eg. you can store all the details of a product in QR Code.

Many payment methods also allow you to scan a QR code and pay money to anyone. Some of the logistics industry heavily uses QR codes to track shipments.

In today’s post, we are going to generate QR Code in Node.js with the popular node package “qrcode”.


1: Install the QR code package via npm

npm install --save qrcode

2: Import the QR Code package in the index.js file

const QRCode = require('qrcode')

3: Define parameters to generate QR Code

const opts = {
 errorCorrectionLevel: 'H',
 type: 'terminal',
 quality: 0.95,
 margin: 1,
 color: {
  dark: '#208698',
  light: '#FFF',
 },
}
const qrImage = await QRCode.toString('Hi testing QR code', opts)

Attributes specifications:

a. Error correction capability allows you to successfully scan a QR Code even if the symbol is dirty or damaged. Four levels are available to choose from according to the operating environment. Higher levels offer a better error resistance but reduce the symbol’s capacity.

b. Color specifies a QR code image color.

c. Type specifies what type of output is expected like image/png, image/jpeg, image/webp in data URL and utf8, SVG, terminal in string.

d. quality specifies the quality of the image in the range of 0–1. The default value is 0.92 & only available for type image/jpeg & image/webp.

e. for the text you can specify the mode of the text like numeric, alphanumeric, Byte & Kanji. Based on the type of mode it will be able to store data like numeric mode can store up to 7,089 characters & byte mode allows 2,953 characters to be stored.

f. version specifies the QR code version in the range of 1–40. If no version is specified, the more suitable value will be used. Each version has a different number of modules (black and white dots), which define the symbol’s size. For version 1 they are 21×21, for version 2 25×25 e so on. Higher is the version, more are the storable data, and of course, bigger will be the QR Code symbol.

Output of the above Code

Methods for QRCode generation:

1. toDataURL to pass into HTML IMG tag.
2. toFile to create file image.
3. toString to create a string representation of the QR Code. If the output format is SVG it will return a string containing XML code.
4. toCanvas to draw QR code symbol to node canvas. You have to pass the document id where canvas should be drawn.
5. create to generate a QR Code symbol and returns a QRcode object.

Check out the different methods of QR code generation in the example here. You can generate any QR code using this module. For example, you can generate a QR Code for your E-commerce product details & store them on AWS S3. Or you can generate a QR Code to your payment portal for quick payment.

Thank you for reading.

Leave a Reply

Your email address will not be published.