How to Generate QR Code in Node JS Tutorial

Share this Article
Reading Time: 4 minutes
1,188 Views

Inside this article we will see the concept of QRCode Generator in Node Js. We will generate QR Code in Node Js. Tutorial will be very easy to understand and also to implement in your code.

If you are looking how to generate qr code in node js, this tutorial will help from scratch.

Node JS is an environment where we can deploy and execute javascript based applications. In this article we will generate qrcode with it.


Premium Courses Running Out, Grab Now!

Master the Coding Skills to Become an Expert in Web Development. A lifetime of knowledge at your fingertips.

Enroll in your favourite web development courses risk-free at $4 only.


Learn More –

Let’s get started.


Node Js Application Setup

Please make sure you have node and npm both installed. To check available versions use these commands.

To check node version

$ node -v

To check npm version

$ npm -v

Create a folder with any name say qrcode. Open this folder into terminal or command prompt.

Next, we need package.json file. Run this given command into terminal

$ npm init -y

The given command will auto generate package.json file with default values.

Next we need to create a file say server.js into node application.


Install qrcode Package

Open project in terminal and run this npm command to install qrcode.

$ npm install qrcode

It will install qrcode npm package to application.

Package details you can read from here.


QRCode Generator

There are few steps we should follow as a beginner.

  • Load qrcode package
  • Prepare data for qrcode
  • create Qr Code image

Let’s see all these in action.

Open server.js file from setup and write this complete code into it.

const qr = require('qrcode');

// Prepare data
let data = {
    id: 1,
    name: "Sanjay",
    email: "testuser@gmail.com"
};

let strData = JSON.stringify(data)

// To display qr code into terminal
qr.toString(strData, { type: 'terminal' },
    function(err, code) {

        if (err) return console.log("error occurred")

        console.log(code)
    });

// To display qrcode in string format
qr.toDataURL(strData, function(err, code) {
    if (err) return console.log("error occurred")

    console.log(code)
})
  

Application Testing

Open project terminal and type this command.

$ node server.js

We hope this article helped you to learn How to Generate QR Code in Node JS Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.