svelte-qrcode icon indicating copy to clipboard operation
svelte-qrcode copied to clipboard

Does this not work for sveltekit?

Open 24jr opened this issue 2 years ago • 12 comments

Getting Uncaught (in promise) SyntaxError: The requested module '/node_modules/svelte-qrcode/src/lib/qrcode/index.js' does not provide an export named 'default'

24jr avatar Aug 28 '21 17:08 24jr

@24jr Take a look at this answer, I believe it can help you

https://github.com/sveltejs/kit/issues/928#issuecomment-817319840

JonasJs avatar Sep 07 '21 02:09 JonasJs

@JonasJs With sveltekit you can create your component library by running

npm run package

Than publish this to NPM . So this component will work in svelte and sveltekit.

No need for extra config in vite

ibis-nreina avatar Jan 24 '22 13:01 ibis-nreina

This still doesn't work in svelte kit :(

elron avatar Jun 22 '22 16:06 elron

yep.. can confirm that this is till broken in svelte-kit :(

vexkiddy avatar Aug 14 '22 12:08 vexkiddy

@JonasJs sorry to nudge this, but any chance of getting this working ? really in need of decent QRcodes in Sveltekit :D

vexkiddy avatar Aug 18 '22 13:08 vexkiddy

@vexkiddy This is component I ended up making for my sveltekit proj. Didnt check rn but I think works

<script>
  import { onMount } from "svelte";

  export let codeValue;
  export let squareSize = "100px";

  $: scriptLoaded, setNewQRcode(codeValue);

  let scriptLoaded = false;
  let qrcode;

  onMount(() => {
    let script = document.createElement("script");
    script.src =
      "https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js";
    document.head.append(script);

    script.onload = function () {
      qrcode = new QRCode("qrcode", {
        text: codeValue,
        width: squareSize,
        height: squareSize,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
    };
  });

  function setNewQRcode(codeVal) {
    if (qrcode && codeVal) {
      qrcode.clear();
      qrcode.makeCode(codeValue);
    }
  }
</script>

<div id="qrcode" />

<style>
  #qrcode {
    display: flex;
    flex-direction: column;
    padding: 1rem;
    border-radius: 1.5rem;
    max-width: clamp(10%, 20rem, 100%);
    margin: auto;
    background: white;
  }
</style>

24jr avatar Aug 18 '22 14:08 24jr

hiya @24jr thanks for this! i've seen this on svelte REPL and it does work, however i don't want to have this external CDN dependancy, would be nice if it was compiled and done at run-time. I'm trying to use the same script but compile it locally, but my svelte-kit knowledge is not good!

vexkiddy avatar Aug 18 '22 15:08 vexkiddy

@vexkiddy yeah I made this a long time ago and prob based off that repl I don't recall but does look a bit odd so yeah I agree with you there. Idk if I tried other methods that didn't work and settled on this or what

24jr avatar Aug 18 '22 15:08 24jr

@24jr i hear you! would be great if @JonasJs could just repackage it as suggested so that it'll work with sveltekit :D

vexkiddy avatar Aug 18 '22 15:08 vexkiddy

just use qrcode with npm i qrcode works in sveltekit

<script>
import qrcode from 'qrcode';
</script>

{#await qrcode.toDataURL('www.google.com') then qrcode}
  <div class="">
    <img src={qrcode} alt="" />
  </div>
{/await}

JunsikChoi avatar Dec 03 '23 16:12 JunsikChoi

Hello, I am developing a new version of the package; I will bring updates soon.

JonasJs avatar Jan 10 '24 12:01 JonasJs

If you are interested, I've created a package that's compatible with both Svelte and SvelteKit.

https://www.npmjs.com/package/@castlenine/svelte-qrcode

Castlenine avatar Jan 10 '24 17:01 Castlenine