How To Generate QR Code In Node.js

In this article, we will see how to generate QR Code in the Node.js application. we will use qrcode npm package to generate QR Code. we will create a JSON data object and create a QR Code for that JSON data object.

QR code has become an important part of life now a days. QR means “Quick Response”. It can store a large amount of data. QR scanners can instantly process the data scanning it. In Node.js generating QR Code is very easy.

So, let's see generate QR code in node js and QR code generator node js.

Step 1: Create Node Application

In this step, create a node application using the below commands.

mkdir qr_code_example

cd qr_code_example

npm init

 

 

Step 2: Install the qrcode package

Install the qrcode module using the below command

npm install qrcode

 

 Step 3: Import the QRCode package into the index.js file

Now, we can import the qrcode package into the index file and generate a QR code.

const qr = require('qrcode');
  
let data = {
    id: 1,
    name: "dell",
    email: "dell@yopmail.com"
};
  
let strData = JSON.stringify(data);
  
qr.toString(strData, {type:'terminal'},
                    function (err, code) {
   
    if(err) return console.log("error occurred !!");
   
    console.log(code);
});
  
qr.toDataURL(strData, function (err, code) {
    if(err) return console.log("error occurred !!");   

    console.log(code);
})

 

 

Define parameters to generate QR Code

const data = {
 errorCorrectionLevel: 'H',
 type: 'terminal',
 quality: 0.95,
 margin: 1,
 color: {
  dark: '#208698',
  light: '#FFF',
 },
}
let strData = JSON.stringify(data)

For Custom QR Code generation you need to define different parameters like the above code.

  1. Error correction capability allows you to successfully scan a QR Code even if the symbol is dirty or damaged.
  2. 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.
  3. Color specifies a QR Code image color.
  4. Type specifies what type of output is expected like image/png, image/jpeg, image/webp in data URL and utf8, SVG, terminal in string.
  5. 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.

 

Step 4: Run the index.js file

Now, run the index.js using the below code.

node index.js

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS