In this post, we are going to learn how to create a QR code in NodeJs. Qr code is a short form of Quick Response code and it can be read quickly by any mobile phone that contains a QR code scanner.
Generating a QR code in NodeJs is super easy then what it looks like. There is a beautiful JavaScript library called node-QRcode available to create QR codes in NodeJs.
We will see here what are the ways you can find to create or generate QR code in NodeJs.
We will use npm to install the node-QRcode library in our project.
Before we start we assume that you have NodeJs installed on your computer and have basic knowledge of Node.
If not then Install NodeJs here.
So let’s get a start and try installing the library.
Install QRcode Library
Navigate to the project folder and install it using the below command.
npm install --save qrcode
The above command will install the node-QRcode library in your project only.
You can also install this library globally to avoid installing again for every individual project that has QRcode functionality.
To install the library globally, use the below command:
npm install -g qrcode
Create QR code in Terminal Using NodeJs
wants to see a generated QR code in a terminal? It’s really easy and you can do so using the code given below:
const express = require('express'); const app = express(); const QRCode = require('qrcode'); const generateQRCode = async content => { try { console.log(await QRCode.toString(content, {type: 'terminal'})) } catch (err) { console.error("Error : ", err) } } // call function generateQRCode("Generate QR code in Terminal using Node.js"); const port = process.env.PORT || 3001; app.listen(port, () => { console.log(`Local: http://localhost:${port}/`); }); module.exports = { app };
and the output will be:
Create QR Code Image Data in NodeJs
You can also create the data URI for QR code which can be used later with an <img> element.
see the code below how you can do it:
const generateQRCode = async url => { try { console.log(await QRCode.toDataURL(url)) } catch (err) { console.error("Error : ", err) } } generateQRCode("https://www.techyhunger.com/");
run the file and the output will be:
node qr-code.js
/* data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJQAAACUCAYAAAB1PADUAAAAAklEQVR4AewaftIAAAUCSURBVO3BQY4cSRIEQdNA/f/Lun30UwCJ9GqSsyaCP1K15KRq0UnVopOqRSdVi06qFp1ULTqpWnRSteikatFJ1aKTqkUnVYtOqhadVC06qVr0yUtAfpOaGyCTmieATGpugExqboBMam6A/CY1b5xULTqpWnRSteiTZWo2AbkB8gSQSc2kZgLyTUAmNTdqNgHZdFK16KRq0UnVok++DMgTap5QcwPkCSA3aiYgN0AmNZuAPKHmm06qFp1ULTqp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RSteikatEny9RsAvKEmgnIpGYCMgG5UTMBuQEyqZmA3Kj5JjWbTqoWnVQtOqla9MmXAXlCzRNAJjVPqLkBMgGZ1NwAeQLIG2r+pJOqRSdVi06qFn3yj1PzBJAbNZvU3Kh5A8gNkEnNN51ULTqpWnRSteiTfxyQSc2k5gbIjZongExqNgG5ATKpuQEyqXnjpGrRSdWik6pFn3yZmm9SMwGZ1ExAJjUTkEnNBGRSM6l5AsgTaiYgk5obIJOaTSdVi06qFp1ULfpkGZDfBGRSswnIpGYCMqm5AXKjZgIyAbkBMqm5ATKpeeOkatFJ1aKTqkX4I1VLTqoWnVQtOqladFK16KRq0UnVopOqRSdVi06qFp1ULTqpWnRSteikatFJ1aKTqkX/A3uYWkXD3N/hAAAAAElFTkSuQmCC */
Conclusion
You can do a lot more than we have seen above.
There are many built-in APIs and options available with the node-QRcode library.
For Browser
- create()
- toCanvas()
- toDataURL()
- toString()
For Server
- create()
- toCanvas()
- toDataURL()
- toString()
- toFile()
- toFileStream()
You can find the detailed instructions of use cases of these APIs mentioned above.
We suggest you go to the node-QRcode library GitHub page and see the options and APIs available there.
Hope this article will help you to create or generate QR code using NodeJs in your project.