Last updated on November 21, 2022
In this article, I’ll walk you, through generating QR codes in Node.js using node-qrcode npm module. A QR Code is a 2-dimensional bar code and it is a machine-readable optical label that contains information about the item to which it is attached.
Let’s install node-qrcode library.
npm install --save qrcode
Generating a QR Code in Node.js
const qrcode = require('qrcode');
const generateQRCode = async text => {
try {
console.log(await qrcode.toDataURL(text))
} catch (err) {
console.error(err)
}
}
generateQRCode("https://arjunphp.com/");
//output:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAACECAYAAABRRIOnAAAAAklEQVR4AewaftIAAAOJSURBVO3BQa7cVgADweaD7n/ljhdZcCVA0My347Aq/sLMvw4z5TBTDjPlMFMOM+UwUw4z5TBTDjPlMFMOM+UwUw4z5TBTDjPl4qUk/CSVO0loKneS0FSeSEJTaUn4SSpvHGbKYaYcZsrFh6l8UhKeUGlJaCpNpSWhqbQkNJUnVD4pCZ90mCmHmXKYKRdfloQnVJ5Iwh2VloQ7Ki0JTaUloak8kYQnVL7pMFMOM+UwUy7+MipvJOEJlb/JYaYcZsphplz85ZJwR6Uloan8nxxmymGmHGbKxZep/E4qLQktCXeScEflDZU/yWGmHGbKYaZcfFgS/iRJaCotCU2lJaGptCQ0lTtJ+JMdZsphphxmysVLKn8ylZaEpvJEEprKHZX/ksNMOcyUw0y5eCkJTaUl4ZNUmkpLwhtJeCIJTaUl4ZNUvukwUw4z5TBTLl5SeUKlJaGp3ElCU2kqn6RyJwktCU2lJaGpPJGEpvJJh5lymCmHmXLxZSpPJOGJJDyh0pLQVFoS7qjcScITSWgqd5LQVN44zJTDTDnMlIsPS0JTeULlThKeUPmdVJ5QuaPSkvBJh5lymCmHmXLxYSotCU2lqbQkfFMS7iShqTyRhDsqLQlNpSXhJx1mymGmHGbKxUtJaCpN5Q2VloQ7KndU7iThiSQ0lTtJaCotCU2lJaGpfNJhphxmymGmXHxZEu6o3ElCU3kjCU8k4Y7KG0l4IwlN5Y3DTDnMlMNMufhhKk+o3ElCU2lJuKNyJwk/SeWOSkvCJx1mymGmHGbKxW+WhDtJaCpN5Y5KS8ITKi0JLQnflISm0lQ+6TBTDjPlMFPiL/yHJaGptCQ0lZaEOyp3ktBUnkjCHZWfdJgph5lymCkXLyXhJ6k0lZaEpnJHpSWhJaGpPJGEpnJHpSXhCZU3DjPlMFMOM+Xiw1Q+KQl3ktBUnkjCHZU3VJ5IQlNpSfimw0w5zJTDTLn4siQ8ofJGEu6o3FFpSXgiCW+otCT8pMNMOcyUw0y5+J9TuaPSkvCGSktCS8LvdJgph5lymCkXfxmVO0m4o3JHpSXhjsodlZaEOyotCZ90mCmHmXKYKRdfpvJNKm+o3EnCG0l4Q+UnHWbKYaYcZsrFhyXhJyWhqXySyhNJ+KQk/KTDTDnMlMNMib8w86/DTDnMlMNMOcyUw0w5zJTDTDnMlMNMOcyUw0w5zJTDTDnMlMNM+QdH0JASz/fXnAAAAABJRU5ErkJggg==
You can just pass the generated string to the image tag to show it to users.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAACECAYAAABRRIOnAAAAAklEQVR4AewaftIAAAOJSURBVO3BQa7cVgADweaD7n/ljhdZcCVA0My347Aq/sLMvw4z5TBTDjPlMFMOM+UwUw4z5TBTDjPlMFMOM+UwUw4z5TBTDjPl4qUk/CSVO0loKneS0FSeSEJTaUn4SSpvHGbKYaYcZsrFh6l8UhKeUGlJaCpNpSWhqbQkNJUnVD4pCZ90mCmHmXKYKRdfloQnVJ5Iwh2VloQ7Ki0JTaUloak8kYQnVL7pMFMOM+UwUy7+MipvJOEJlb/JYaYcZsphplz85ZJwR6Uloan8nxxmymGmHGbKxZep/E4qLQktCXeScEflDZU/yWGmHGbKYaZcfFgS/iRJaCotCU2lJaGptCQ0lTtJ+JMdZsphphxmysVLKn8ylZaEpvJEEprKHZX/ksNMOcyUw0y5eCkJTaUl4ZNUmkpLwhtJeCIJTaUl4ZNUvukwUw4z5TBTLl5SeUKlJaGp3ElCU2kqn6RyJwktCU2lJaGpPJGEpvJJh5lymC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