{"id":16185,"library":"qr-scanner","title":"Lightweight QR Code Scanner","description":"qr-scanner is a JavaScript library designed for efficient QR code scanning from both continuous video streams (webcams) and static images. Currently at version 1.4.2, it appears to be actively maintained, supported by Nimiq, and offers significant performance and accuracy advantages over older libraries like LazarSoft/jsqrcode, boasting a 2-8x higher detection rate and fewer misreads according to benchmarks. Key differentiators include its lightweight footprint (as low as 5.6 kB gzipped when `BarcodeDetector` is available), automatic utilization of the browser's native `BarcodeDetector` API for optimal performance, and its design to run in a WebWorker to keep the main UI thread responsive. The library is built upon Cosmo Wolfe's JavaScript port of Google's ZXing library, with several improvements geared towards modern web environments and optimized for colored QR codes.","status":"active","version":"1.4.2","language":"javascript","source_language":"en","source_url":"https://github.com/nimiq/qr-scanner","tags":["javascript","qr","scanner","reader","js","lightweight","small","fast","typescript"],"install":[{"cmd":"npm install qr-scanner","lang":"bash","label":"npm"},{"cmd":"yarn add qr-scanner","lang":"bash","label":"yarn"},{"cmd":"pnpm add qr-scanner","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the primary ESM import for bundlers like Webpack or Rollup. Ensure your project is configured for module resolution.","wrong":"const QrScanner = require('qr-scanner');","symbol":"QrScanner","correct":"import QrScanner from 'qr-scanner';"},{"note":"For direct ES Module usage in a browser `<script type=\"module\">`, specify the exact path to the distributed file. It's a default export, not named.","wrong":"import { QrScanner } from 'qr-scanner';","symbol":"QrScanner","correct":"import QrScanner from './path/to/qr-scanner.min.js';"},{"note":"For CommonJS environments (e.g., Node.js or older bundlers like Browserify), use the UMD build explicitly. The UMD build exposes `QrScanner` as a global if loaded via a `<script>` tag.","wrong":"import QrScanner from 'qr-scanner/qr-scanner.umd.min.js';","symbol":"QrScanner","correct":"const QrScanner = require('qr-scanner/qr-scanner.umd.min.js');"}],"quickstart":{"code":"<!-- index.html -->\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>QR Scanner Demo</title>\n</head>\n<body>\n    <h1>Scan QR Code</h1>\n    <video id=\"qr-video\" style=\"width: 100%; max-width: 400px;\"></video>\n    <div id=\"qr-result\"></div>\n\n    <script type=\"module\">\n        import QrScanner from 'qr-scanner'; // Adjust path if not using a bundler\n\n        // You need to copy qr-scanner-worker.min.js to a public path or configure your bundler\n        // If qr-scanner-worker.min.js is in the same directory as this script:\n        QrScanner.workerScript = './qr-scanner-worker.min.js';\n        // If using npm and a bundler, it might be resolved automatically or you might need to copy it.\n        // If deployed to a 'dist' folder, and worker is in 'dist/':\n        // QrScanner.workerScript = '/dist/qr-scanner-worker.min.js';\n\n        const videoElement = document.getElementById('qr-video');\n        const resultElement = document.getElementById('qr-result');\n\n        const qrScanner = new QrScanner(\n            videoElement,\n            result => {\n                resultElement.textContent = `QR Code detected: ${result.data}`;\n                console.log('QR Code detected:', result.data);\n                // To stop scanning after first detection:\n                // qrScanner.stop();\n            },\n            { \n                /* your options or pass nothing */ \n                highlightScanRegion: true,\n                highlightCodeOutline: true,\n                returnDetailedScanResult: true\n            }\n        );\n\n        // Start scanning immediately when the page loads\n        qrScanner.start().then(() => {\n            console.log('QR Scanner started');\n        }).catch(err => {\n            console.error('Failed to start QR Scanner:', err);\n            resultElement.textContent = `Error starting scanner: ${err.message || err}`;\n        });\n\n        // Optional: Stop scanner when component unmounts or page navigates\n        // window.addEventListener('beforeunload', () => {\n        //     qrScanner.stop();\n        // });\n\n    </script>\n</body>\n</html>\n","lang":"typescript","description":"Demonstrates how to set up `qr-scanner` to read QR codes from a live webcam feed in a browser environment, displaying the result on the page. It highlights the essential worker script configuration."},"warnings":[{"fix":"Ensure `qr-scanner-worker.min.js` is in your public assets directory and set `QrScanner.workerScript = 'path/to/qr-scanner-worker.min.js';` before initializing `QrScanner`. For bundlers, consult your bundler's documentation on how to handle web worker scripts or static assets.","message":"The `qr-scanner-worker.min.js` file is loaded dynamically and *must* be accessible at the path specified by `QrScanner.workerScript`. If you're not using a bundler that handles web workers (like some versions of Webpack with appropriate loaders), or if your bundler doesn't copy the file to the correct output directory, you will need to manually copy `qr-scanner-worker.min.js` to a public path where your main script can find it.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For CommonJS environments or older build setups, explicitly use `require('qr-scanner/qr-scanner.umd.min.js')`. For modern browser usage or bundlers, use `import QrScanner from 'qr-scanner';` within a `<script type=\"module\">` or an ES module-aware bundler setup.","message":"The primary `qr-scanner` package is distributed as an ES Module. Attempting to `require('qr-scanner')` directly in a CommonJS context without a bundler that transpiles ESM to CJS, or without explicitly importing the UMD build (`qr-scanner.umd.min.js`), will lead to module loading errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Integrate a JavaScript transpiler like Babel into your build pipeline and configure it to target older browsers with necessary polyfills for ES2017 features.","message":"The library utilizes ECMAScript 2017 features, including `async` functions. If targeting older browsers that do not natively support these features, you may need to include appropriate polyfills (e.g., via Babel) in your project to ensure compatibility.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Inform users that performance might differ across browsers. No direct 'fix' other than using a modern browser. You can check `window.BarcodeDetector` for its availability if you need to provide browser-specific UI/UX hints.","message":"The performance and capabilities of `qr-scanner` can vary significantly based on browser support for the native `BarcodeDetector` API. While the library falls back to its internal engine, optimal performance (including smaller bundle size) is achieved when `BarcodeDetector` is available.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change your `<script>` tag to `<script type=\"module\">` or load the UMD build (`qr-scanner.umd.min.js`) directly via a standard `<script>` tag, which exposes `QrScanner` globally.","cause":"Attempting to use `import` syntax in a JavaScript file that is not treated as an ES module (e.g., a standard `<script>` tag without `type=\"module\"`).","error":"Uncaught SyntaxError: Cannot use import statement outside a module"},{"fix":"Verify that `qr-scanner-worker.min.js` is physically located in your public assets directory, that `QrScanner.workerScript` is set to the correct URL relative to your HTML page or domain root, and that your web server serves `.js` files with the `application/javascript` MIME type.","cause":"The browser could not find or load the web worker script (`qr-scanner-worker.min.js`) at the specified path, often due to incorrect file placement, an incorrect `QrScanner.workerScript` path, or a server configuration issue.","error":"Failed to load module script: The server responded with a non-JavaScript MIME type of \"\" (or 404 Not Found) for \"[path]/qr-scanner-worker.min.js\"."},{"fix":"Ensure you are using the correct import strategy for your environment: `import QrScanner from 'qr-scanner';` for ES Modules (with bundler), `import QrScanner from './path/to/qr-scanner.min.js';` for plain browser ES Modules, or `const QrScanner = require('qr-scanner/qr-scanner.umd.min.js');` for CommonJS.","cause":"This usually occurs when `QrScanner` is not correctly imported or exposed in the current scope. Common causes include using `require()` on an ES module, or importing a UMD build that expects global access.","error":"TypeError: QrScanner is not a constructor"}],"ecosystem":"npm"}