How to Generate QR Code in Node.js

In this article, we will learn how to generate QR code in Node.js using a simple and powerful package. QR codes are widely used today in websites, businesses, payments, and events to make sharing information quick and effortless. With just a scan, users can open a link, access a service, or complete a transaction.

Using Node.js, you can easily create QR codes for your applications. This guide will explain step by step how to generate QR code in Node.js with practical real-world examples using the URL Codewolfy.

Why Do We Need QR Code Generation?

QR codes are one of the fastest and most reliable ways to share digital information. Instead of typing long URLs or details, a single scan instantly gives access to the data. Businesses use QR codes for product packaging, restaurants use them for digital menus, and e-commerce platforms use them for payments. That is why QR code generation in Node.js is highly useful and easy to implement for developers creating modern web and mobile applications.

Generating QR codes in Node.js is fast, lightweight, and easy to integrate. It works in both backend and frontend, supports formats like PNG, SVG, and Data URL, allows customization with colors and error correction, and fits perfectly into real-time applications.

Installing the QR Code Package Node.js

To begin, we need the qrcode npm package. It is the most popular and reliable package to generate QR codes in Node.js.

npm install qrcode

Once installed, you can start creating different types of QR codes in your project. In further post, you will see how to implement or generate different types of QR code as per your requirements.

Generate a Basic QR Code in Node.js

A basic QR code is the most common type, often used for sharing website links. For example, if you want to guide users directly to your site, you can generate a QR code that redirects to Our website: https://codewolfy.com.

const QRCode = require('qrcode')

QRCode.toFile('basic_qrcode.png', 'https://codewolfy.com/')

//or

QRCode.toFile('qrcode.svg', 'https://codewolfy.com/')

As you can see here, you can export QR code into different image format like png or SVG with just providing different file name with extension.

Generate a QR Code as a Data URL

If you want to display a QR code inside a web page without saving it as a file, you can generate a Data URL. This is useful for embedding QR codes in websites, dashboards, or emails.

const QRCode = require('qrcode')

QRCode.toDataURL('https://codewolfy.com/')
  .then(url => console.log(url))

It will produces a Base64 encoded string which can be used directly as the src of an tag in HTML. It’s a common format for showing QR code at run time for different use cases like social media profiles, unique payments or any other custom requirements.

Generate a QR Code in the Terminal

For quick testing or demonstrations, you can display a QR code directly inside the terminal. This is helpful for developers who want to preview the QR code instantly without opening a file.

It can be very useful when you need to check output before implementing it to entire application. You can try different combination and decide which method fulfill your needs for Node.js application.

const QRCode = require('qrcode')

QRCode.toString('https://codewolfy.com/', { type: 'terminal' })
  .then(qr => console.log(qr))

Unlike other methods, it prints a QR code made of ASCII characters inside the console.

Generate a Colored QR Code in Node.js

Brands often want QR codes that match their design. Instead of the standard black and white, you can create colorful QR codes to fit your style or branding strategies.

const QRCode = require('qrcode')

QRCode.toFile('colored_qrcode.png', 'https://codewolfy.com/', {
  width: 1000,
  color: {
    dark: '#f85151',
    light: '#ffffff'
  }
})

This creates a QR code with a red foreground and white background. It looks more engaging while still being scannable. Now days, most of the organization prefers this type of QR code with more configuration options like high error corrections.

Colored QR code generation in node.js

You can also provide size for output image like width and height. In above example, you can see we have assigned 1000 as width for generated QR code.

Generate a QR Code with High Error Correction

For marketing or outdoor posting of QR code sometimes it can be damaged, to fix this issue and provide high availability even QR code is destroy 50% to 60% you can use High error correction. It makes QR codes readable even if they are scratched, folded, or partially covered.

const QRCode = require('qrcode')

QRCode.toFile('qrcode_high_error.png', 'https://codewolfy.com/', {
  errorCorrectionLevel: 'H'
})

This ensures your QR code works reliably in real-world conditions like outdoor posters, packaging, or physical marketing campaigns.

Conclusion

Now you know how to generate QR code in Node.js. From basic PNG QR codes to advanced styles like SVG, Data URLs, colored codes, terminal outputs, and high error correction, the QRCode package makes everything simple.

Whether you are building websites, e-commerce apps, or marketing campaigns, QR code generation with Node.js is a fast and powerful solution.

The QRCode package also provides other useful methods like generating QR codes as streams or displaying them directly in the terminal. These features can be very handy in real-life applications, but to keep this article focused and beginner-friendly, we have covered only the basic methods here.