How to Generate QR Code in Node.js Example

Sovary July 23, 2022 2.07K
2 minutes read

QR code is commonly use for bank payment and in goods store. The pixel dot image which human can not read, but contain rich amount of information. Today tutorial we are going to implement how to generate QR Code in Node.js. We will use npm package called "qrcode" to generate QR Code.

So let's follow the step below to create CQ generator in Node.js

How to Generate QR Code in Node.js

Following step to generate qr code in node.js

  • Step 1 - Create Node.js App
  • Step 2 - Install QR Code Package
  • Step 3 - Create index.js
  • Step 4 - Run index.js

Step 1 - Create Node.js App 

Suppose you have create an empty directory, then launch terminal navigate to your current directory and run command below:

npm init

Step 2 - Install QR Code Package

Run command below to install npm QR Code package.

npm install --save qrcode

Step 3 - Create index.js

We are going to import the package to index.js and implement generator QR code.

Create file in root directory index.js

const qr = require('qrcode');
let data = 
{
    id: 120,
    name: "Xiao Xing",
    email: "user@cambotutorial.com",
    gender: "male",
    department: "Investigation"
};

let strJson = JSON.stringify(data);
const config = {type:'terminal'};

// Display QR code to terminal
qr.toString(strJson, config, function(err, code)
{
    if(err) return console.log("error occurred");
    console.log(code);
});

// Display QR code in base64 string  
qr.toDataURL(strJson, function (err, code) 
{
    if(err) return console.log("error occurred");
    console.log(code)
});

Config Parameter QR Code - Example

let config = {
  errorCorrectionLevel: 'H',
  type: 'terminal',
  quality: 0.95,
  margin: 1,
  color: {
    dark:"#010599FF",
    light:"#ff8c8c"
  }
};
qr.toString(strJson, config, function(err, code){});

There are property to configure generate QR code for this example is specifiy for function .toString():

  • errorCorrectionLevel: Error correction capability allows to successfully scan a QR Code even if the symbol is dirty or damaged. Supported value (L, M, Q, H).
  • type: Output format. Supported value (terminal,utf8,svg).
  • quality: A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
  • margin: Define how much wide the quiet zone should be.
  • color.dark: Ouput dot color.
  • color.light: Transparant background qr code.

You can check for detail with different function qrcode

Step 4 - Run index.js

That's it. Now let's run command to launch index.js and see the result.

node index.js

generate QRCode in Node.js

Hope this short article help you. Have a nice day!

Source code: https://youtu.be/dEwtRmfDS9w

You might Also Like:

Node.js 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him