JavaScript

Generating a QR code using Node.js

Advertisements

I was preparing a slide deck for a hackathon and decided to put in a QR code to allow people to connect with me. Naturally, I googled for a service and used it. It turns out that the code generated used a link that had an expiration time associated with it.

I learned my lesson and searched for an npm package to achieve this for the future. With the terminal, I would be in total control next time by generating a QR code using Node.js instead.

And all good things start with installing an npm package. In this case, we can either install the qrcode package globally or use our good old friend npx to install it in a temporary cache. We will then use a shell script that makes use of this npm package for generating a QR code using Node.js in the terminal. I will talk about the shell script first, and then combine it with the npx usage as a single command towards the end.

Firstly, we will take the user input we want to use as text for our QR code. We will also store this in a variable called inputText

read -p "Enter the text for the QR code: " inputText
Bash

Next, we will pass this input to the qrcode npm package

qrcode -o qr.png "$inputText"
Bash

The -o option is used to specify the output file name that we want it to generate. Along with that, we will pass a couple of other arguments to the package

-w 1000 -q 1
Bash

The w parameter is used to specify the width of the output image and the q flag is used to specify the quality of the image (1 is the highest).

So combining these together into one command, we can use the following for generating a QR code using Node.js

read -p "Enter input text for the QR code: " inputText && qrcode -o qr.png "$inputText" -w 1000 -q 1
Bash

The above code works well if we have installed the qrcode package globally. But if we want to use npx, we can modify the command as follows:

read -p "Enter the text for the QR code: " inputText && npx qrcode -o qr.png "$inputText" -w 1000 -q 1
Bash

And that is all there is to it. This should generate a QR code that we can use wherever we want without worrying about expiration time concerns. If you have any questions regarding the process, feel free to ask them in the comments below.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 week ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

4 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

6 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

7 months ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

7 months ago
Advertisements