Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Md-Anamul-Haque opened this issue Jan 31, 2024 · 11 comments
Labels
info needed Extra information is needed to address the issue

Comments

@Md-Anamul-Haque
Copy link

Md-Anamul-Haque commented Jan 31, 2024

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 }
              });
@catdad
Copy link
Owner

catdad commented Feb 2, 2024

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

@catdad catdad added the info needed Extra information is needed to address the issue label Feb 2, 2024
@Yousuf-H
Copy link

Yousuf-H commented Feb 6, 2024

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.

@catdad
Copy link
Owner

catdad commented Feb 6, 2024

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.

@GP-95
Copy link

GP-95 commented Feb 12, 2024

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.

@Md-Anamul-Haque
Copy link
Author

you can export the default confetti form canvas-confetti.

@richardoey
Copy link

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

@Md-Anamul-Haque
Copy link
Author

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').

@ulitcos
Copy link
Contributor

ulitcos commented Mar 20, 2024

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

@catdad
Copy link
Owner

catdad commented Mar 20, 2024

@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?

@Md-Anamul-Haque
Copy link
Author

Md-Anamul-Haque commented Apr 14, 2024

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

@AdamPro13
Copy link

AdamPro13 commented May 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info needed Extra information is needed to address the issue
Projects
None yet
Development

No branches or pull requests

7 participants