canvas-confetti icon indicating copy to clipboard operation
canvas-confetti copied to clipboard

Attempted import error: 'canvas-confetti' does not contain a default export (imported as 'confetti'). in nextjs

Open Md-Anamul-Haque opened this issue 1 year ago • 13 comments

Attempted import error: 'canvas-confetti' does not contain a default export (imported as 'confetti'). in nextjs

import confetti from "canvas-confetti";

//---------  ** ---------

      confetti({
                  particleCount: 100,
                  spread: 70,
                  origin: { y: 0.6 }
              });

Md-Anamul-Haque avatar Jan 31 '24 07:01 Md-Anamul-Haque

Hmm... I know other people are successfully using this module in their nextjs project. Are you fully describing the problem you are having?

catdad avatar Feb 02 '24 05:02 catdad

I was having the same issue when using this in my rails app. I managed to fix it by doing import "canvas-confetti";

for example:

import "canvas-confetti";

//--------- ** ---------

confetti({ particleCount: 100, spread: 70, origin: { y: 0.6 } });

Hopefully that helps.

Yousuf-H avatar Feb 06 '24 07:02 Yousuf-H

Just to make sure there isn't anything I don't understand, I just created a new app using npx create-next-app. Then I created the following component:

"use client";
import confetti from "canvas-confetti";

export const ConfettiButton = () => {
    return <button onClick={() => {
        confetti();
    }}>click for confetti</button>
};

Then I used this button without an issue, never receiving an error.

I don't doubt that y'all are having an issue, but the repro steps seem non-obvious. Please describe how I can reproduce the issue. Providing a minimal repro app is usually the best way to make sure an issue gets looked at.

catdad avatar Feb 06 '24 14:02 catdad

I was having the same issue when using this in my rails app. I managed to fix it by doing import "canvas-confetti";

for example:

import "canvas-confetti";

//--------- ** ---------

confetti({ particleCount: 100, spread: 70, origin: { y: 0.6 } });

Hopefully that helps.

When using Typescript then make sure 'allowUmdGlobalAccess' is enabled in tsconfig.

GP-95 avatar Feb 12 '24 12:02 GP-95

you can export the default confetti form canvas-confetti.

Md-Anamul-Haque avatar Feb 27 '24 09:02 Md-Anamul-Haque

I was having the same issue when using this in my rails app. I managed to fix it by doing import "canvas-confetti"; for example: import "canvas-confetti"; //--------- ** --------- confetti({ particleCount: 100, spread: 70, origin: { y: 0.6 } }); Hopefully that helps.

When using Typescript then make sure 'allowUmdGlobalAccess' is enabled in tsconfig.

I'm using NextJS v14.1.0 and Typescript. I solve it by doing npm i --save-dev @types/canvas-confetti

circlensq avatar Mar 03 '24 01:03 circlensq

Just to make sure there isn't anything I don't understand, I just created a new app using npx create-next-app. Then I created the following component:

"use client";
import confetti from "canvas-confetti";

export const ConfettiButton = () => {
    return <button onClick={() => {
        confetti();
    }}>click for confetti</button>
};

Then I used this button without an issue, never receiving an error.

I don't doubt that y'all are having an issue, but the repro steps seem non-obvious. Please describe how I can reproduce the issue. Providing a minimal repro app is usually the best way to make sure an issue gets looked at.

try to build then you must see Attempted import error: 'canvas-confetti' does not contain a default export (imported as 'confetti').

Md-Anamul-Haque avatar Mar 20 '24 17:03 Md-Anamul-Haque

@Md-Anamul-Haque, try using the react version - https://github.com/ulitcos/react-canvas-confetti

ulitcos avatar Mar 20 '24 18:03 ulitcos

@Md-Anamul-Haque I don't understand what you mean by "try to build then you must see". I did build it. It worked fine. Builds without error, the web page shows a button and clicking that button launches confetti.

Do you have an application that you have built that you can show here to help with debugging?

catdad avatar Mar 20 '24 18:03 catdad

you use now typescript + nodejs and build then you see this morning

Md-Anamul-Haque avatar Apr 14 '24 05:04 Md-Anamul-Haque

I'm running into this issue after upgrading my app from Next.js 14.0.4 to 14.1+. I've been able to reproduce this with Next.js 14.1.1 and 14.2.3.

If I create a new sample Next.js app using Next.js version 14.2.3 (the latest), my confetti component with the exact same code as my real app is able to render in this sample app. I also tried moving my confetti component to be a child of my real application's root, (trying to isolate it from something strange that could be happening upstream in the tree) but that also doesn't work.

I'm pretty stumped as to why I'm able to use this component at the root of my demo app but not my real app.

'use client'

import { useEffect } from 'react'
import confetti from 'canvas-confetti'

const Confetti = () => {
    useEffect(() => {
        // Confetti trigger logic goes here
        const triggerConfetti = () => {
            const durationMs = 5 * 1000
            const animationEnd = Date.now() + durationMs
            const defaults = {
                startVelocity: 40,
                spread: 360,
                ticks: 85,
                zIndex: 0,
            }

            function randomInRange(min: number, max: number) {
                return Math.random() * (max - min) + min
            }

            const interval: ReturnType<typeof setInterval> = setInterval(
                function () {
                    const timeLeft = animationEnd - Date.now()

                    if (timeLeft <= 0) {
                        return clearInterval(interval)
                    }

                    var particleCount = 50 * (timeLeft / durationMs)
                    confetti(
                        Object.assign({}, defaults, {
                            particleCount,
                            origin: {
                                x: randomInRange(0.1, 0.9),
                                y: Math.random() - 0.2,
                            },
                            scalar: 2,
                        })
                    )
                },
                250
            )
        }

        triggerConfetti()
    }, [])

    return null
}

export default Confetti

AdamPro13 avatar May 16 '24 00:05 AdamPro13

Hmm... I know other people are successfully using this module in their nextjs project. Are you fully describing the problem you are having?

'use client' import confetti from 'canvas-confetti'; image

l-chin avatar Jul 02 '24 08:07 l-chin

I think something with Typescript ? are you picking typescript when setting up with npx create-next-app

angelayanpan avatar Jul 26 '24 21:07 angelayanpan