Vue QR Code Component
vue-qrcode is a Vue.js component designed for generating QR codes directly within Vue applications. It serves as a declarative wrapper around the popular `qrcode` library (github.com/soldair/node-qrcode), abstracting away the complexities of direct canvas or SVG manipulation. The package is currently at version 2.2.2 and maintains an active development cycle, with recent patch and minor releases addressing fixes and new features. It differentiates itself by offering a Vue-idiomatic interface to robust QR code generation, supporting a wide array of customization options including version, error correction level, quiet zone margin, scaling, exact width, and customizable dark and light module colors. It's compatible with both Vue 2.7+ and Vue 3.0+ environments, making it suitable for modern Vue projects.
Common errors
-
[Vue warn]: Unknown custom element: <vue-qrcode> - did you register the component correctly?
cause The `VueQrcode` component was used in a template but was not properly registered with Vue, either locally within the parent component or globally.fixEnsure `VueQrcode` is imported and added to the `components` option of your Vue component, e.g., `components: { VueQrcode }`. For global registration in Vue 3, use `app.component('VueQrcode', VueQrcode)`. -
TypeError: Cannot read properties of undefined (reading 'create') (or similar error originating from 'qrcode' module)
cause The `qrcode` peer dependency, which `vue-qrcode` relies on for core functionality, is either not installed or an incompatible version is present in your project.fixInstall the required `qrcode` package: `npm install qrcode` or `yarn add qrcode`. Verify its version meets the `vue-qrcode` peer dependency requirement. -
Property 'value' is missing in type '{ ... }' but required in type 'VueQrcodeProps'. (TypeScript error)cause When using TypeScript, the `value` prop for the `VueQrcode` component is mandatory and was not provided in the template.fixAlways provide the `value` prop to the `<vue-qrcode>` component, as it defines the data to be encoded. Example: `<vue-qrcode :value="myQrCodeData" />`. -
Module not found: Error: Can't resolve 'vue-qrcode' (or 'require() of ES Module ... is not supported')
cause Your build setup or Node.js environment is attempting to load `vue-qrcode` as a CommonJS module, but it's primarily designed for ESM, especially in newer versions.fixEnsure your bundler is configured to handle ESM modules. If in Node.js, confirm your project's `package.json` has `"type": "module"` if you are using ESM syntax, or consider dynamic `import()` for CJS contexts.
Warnings
- breaking Version 2.0.0 of `vue-qrcode` introduced a breaking change by raising the minimum required Vue version to 2.7.0 or 3.0.0+. Projects using older Vue versions will need to upgrade Vue or remain on `vue-qrcode` v1.x.
- breaking Starting from version 2.1.0, `vue-qrcode` upgraded its internal `qrcode` dependency to v1.5.0+. This update might introduce subtle rendering changes or expose new features/deprecations from the underlying `qrcode` library that could affect existing implementations.
- gotcha The `qrcode` library is a mandatory peer dependency. If it is not installed in your project, or an incompatible version is present, `vue-qrcode` will likely fail at runtime or during the build process with errors related to missing `qrcode` modules.
- gotcha As of `vue-qrcode` v2.2.1, ESM entries were added to the `package.json`. While this generally improves compatibility with modern bundlers and environments (like Vite), ensure your project's build configuration correctly handles ESM imports. In some older or mixed CJS/ESM setups, this could lead to module resolution issues.
Install
-
npm install vue-qrcode -
yarn add vue-qrcode -
pnpm add vue-qrcode
Imports
- VueQrcode
import { VueQrcode } from 'vue-qrcode'import VueQrcode from 'vue-qrcode'
- VueQrcode
const VueQrcode = require('vue-qrcode')import VueQrcode from 'vue-qrcode'
- QRCodeSegment
import type { QRCodeSegment } from 'vue-qrcode'
Quickstart
<template>
<vue-qrcode
value="https://www.1stg.me"
:options="{ errorCorrectionLevel: 'H' }"
@change="onDataUrlChange"
/>
</template>
<script>
import VueQrcode from 'vue-qrcode'
export default {
data() {
return {
dataUrl: null,
qrValue: 'https://www.example.com/qr-data'
}
},
components: {
VueQrcode,
},
methods: {
onDataUrlChange(dataUrl) {
console.log('Generated Data URL:', dataUrl)
this.dataUrl = dataUrl
},
updateQrValue() {
this.qrValue = `https://new-data.com/${Date.now()}`
}
},
mounted() {
// Simulate updating QR code value after some time
setTimeout(this.updateQrValue, 5000)
}
}
</script>