QR Code Encoding (Legacy)
qr.js is a minimalist JavaScript library designed for encoding QR codes directly within the browser or Node.js environments. Published as version 0.0.0, the package has been unmaintained since its last update in November 2015. It is notable for its pure JavaScript implementation with no external dependencies, repackaging work from a Japanese qrcode library. Despite its initial purpose and wide adoption in older projects, its lack of maintenance and critical security issues, including a repository hijacking incident, make it unsuitable for new development. Developers are strongly advised to consider modern, actively maintained alternatives for QR code generation to ensure security and compatibility.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('qr.js')` in an ES module (`.mjs` file or `type: "module"` in `package.json`) environment.fixThis package is CommonJS only. Either convert your project to use CommonJS (not recommended for new projects), or use a bundler that handles CJS-ESM interop. The recommended fix is to replace `qr.js` with a modern QR code library that supports ESM. -
TypeError: qr is not a function
cause Incorrectly attempting to instantiate `qr` with `new` (e.g., `new qr(...)`) or calling it with no arguments. The `qr` import directly returns a function.fixCall `qr` as a regular function: `const qrcodeData = qr('your input string');`. -
Cannot find module 'qr.js'
cause This error could stem from incorrect installation, a typo in the package name, or, historically, issues related to the original GitHub repository's hijacking if you were relying on a direct git URL.fixEnsure `qr.js` is correctly installed via `npm install qr.js`. If you were linking to a GitHub URL, update your dependencies to use the npm package or, preferably, switch to a different, maintained QR code library.
Warnings
- breaking The original GitHub repository (shtylman/qr.js) was subject to a hijacking incident in late 2021. If your project directly linked to this specific GitHub URL as a dependency (e.g., via `git+https://github.com/shtylman/qr.js`), your builds would have failed or potentially pulled malicious code at the time.
- gotcha The package is published as version 0.0.0 and has not been updated since November 2015. This indicates a complete lack of maintenance, no semantic versioning, and potential incompatibility with modern JavaScript environments or security practices.
- deprecated qr.js strictly uses CommonJS `require()` syntax. It does not provide native ESM exports, which can lead to compatibility issues in modern JavaScript module environments that primarily use `import`/`export`.
- gotcha This library only provides the raw QR code data (a 2D array of boolean cells) and does not include rendering capabilities (e.g., to canvas, SVG, or image files) out of the box. You would need to implement rendering logic yourself.
Install
-
npm install qr.js -
yarn add qr.js -
pnpm add qr.js
Imports
- qr
import qr from 'qr.js';
const qr = require('qr.js'); - qrcode
import { qrcode } from 'qr.js'; import qrcode from 'qr.js';const qrcode = require('qr.js'); const cells = qrcode('your data').modules;
Quickstart
const qr = require('qr.js');
// Data to encode in the QR code
const data = 'Hello, Checklist Day!';
// Generate the QR code data structure
const qrcode = qr(data);
// Access the QR code cells (modules)
const cells = qrcode.modules;
console.log(`QR Code for: "${data}"`);
console.log(`Version: ${qrcode.typeNumber}`);
console.log(`Error Correction Level: ${qrcode.correctLevel}`);
console.log(`Number of cells (rows): ${cells.length}`);
console.log(`First row of cells: ${cells[0].map(cell => cell ? '██' : ' ').join('')}`);
// Example: Manually printing a small part of the QR code
for (let r = 0; r < Math.min(5, cells.length); r++) {
let rowOutput = '';
for (let c = 0; c < Math.min(10, cells[r].length); c++) {
rowOutput += cells[r][c] ? '██' : ' ';
}
console.log(rowOutput);
}