Generate QR codes in browsers and Node.js. Complete guide with React, Vue, Angular integration examples.
| Library | Size | Use Case | Browser/Node |
|---|---|---|---|
| qrcode.js | 2.8 KB | Browser - lightweight | Browser |
| qrcode | 7 KB | Universal - Node & Browser | Both |
| qrcode.react | 5 KB | React components | Browser |
| jsQR | 3 KB | Reading QR codes | Browser |
npm install qrcode
<html>
<body>
<div id="qrcode"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode.js/1.0.0/qrcode.min.js"></script>
<script>
new QRCode(document.getElementById("qrcode"), "https://example.com");
</script>
</body>
</html>
import QRCode from 'qrcode';
QRCode.toCanvas(
document.getElementById('canvas'),
'https://qrcodegeneratorjp.com',
{
color: {
dark: '#185FA5',
light: '#ffffff'
},
width: 300,
errorCorrectionLevel: 'H'
},
(error) => {
if (error) console.error(error);
console.log('QR code generated!');
}
);
import QRCode from 'qrcode';
QRCode.toDataURL('https://example.com', {
errorCorrectionLevel: 'H',
type: 'image/png',
quality: 0.95,
margin: 1,
color: {
dark: '#000000',
light: '#FFFFFF'
}
})
.then(url => {
const link = document.createElement('a');
link.href = url;
link.download = 'qrcode.png';
link.click();
});
import QRCode from 'qrcode.react';
function MyQRCode() {
const ref = React.useRef();
const downloadQR = () => {
const canvas = ref.current.querySelector('canvas');
const url = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = 'qrcode.png';
link.href = url;
link.click();
};
return (
<div>
<QRCode
ref={ref}
value="https://example.com"
size={256}
bgColor="#ffffff"
fgColor="#000000"
level="H"
includeMargin={true}
/>
<button onClick={downloadQR}>Download QR</button>
</div>
);
}
import { useState } from 'react';
import QRCode from 'qrcode.react';
export default function QRGenerator() {
const [value, setValue] = useState('https://example.com');
const [color, setColor] = useState('#000000');
return (
<div>
<input
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Enter URL or text"
/>
<input
type="color"
value={color}
onChange={(e) => setColor(e.target.value)}
/>
<QRCode value={value} fgColor={color} />
</div>
);
}
<template>
<div>
<input v-model="url" placeholder="Enter URL">
<qrcode-vue :value="url" :options="options"></qrcode-vue>
</div>
</template>
<script>
import QrcodeVue from 'qrcode.vue';
export default {
components: { QrcodeVue },
data() {
return {
url: 'https://example.com',
options: {
color: {
dark: '#185FA5',
light: '#ffffff'
},
width: 300
}
};
}
};
</script>
const express = require('express');
const QRCode = require('qrcode');
const app = express();
app.get('/generate', async (req, res) => {
try {
const { data, size = 300 } = req.query;
const qrCode = await QRCode.toDataURL(data, {
width: size,
color: {
dark: '#185FA5',
light: '#ffffff'
},
errorCorrectionLevel: 'H'
});
res.json({ qrCode });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => console.log('QR API running on :3000'));
const QRCode = require('qrcode');
const fs = require('fs');
async function generateQR() {
try {
await QRCode.toFile('qrcode.png', 'https://example.com', {
color: {
dark: '#000000',
light: '#FFFFFF'
},
width: 300,
margin: 1,
errorCorrectionLevel: 'H'
});
console.log('QR code saved!');
} catch (error) {
console.error(error);
}
}
generateQR();
L - 7% error correction (indoor use)M - 15% error correction (standard)Q - 25% error correction (outdoor use)H - 30% error correction (print + logo)// Memoize generated QR codes in React
import { useMemo } from 'react';
function OptimizedQR({ data }) {
const qrCode = useMemo(() => data, [data]);
return <QRCode value={qrCode} />;
}
A: Use error correction level 'H' and test with multiple apps. Some apps have bugs.
A: Use Worker threads in Node.js or Web Workers in browser for parallel processing.
A: Use Canvas API after generation to overlay logo image.
A: Yes! JavaScript libraries work completely offline without server dependency.