Jump to content
Search Community

gsap nuxtjs plugin

gregor test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi,

 

maybe someone has the same problem with nuxtjs. I wanted to shorten the import of gsap and scrolltrigger a bit and used a plugin. It works locally, but when I generate the pages, I just get this error.

[error] [nuxt] [request error] [unhandled] [500] gsap$2.registerPlugin is not a function

 

this is the plugin

import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'

gsap.registerPlugin(ScrollTrigger)

export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.vueApp.use(gsap)
    nuxtApp.provide('gsap', gsap)
    nuxtApp.provide('ScrollTrigger', ScrollTrigger)
})

now I can use this line to have access to gsap and scrolltrigger

const { $gsap, $ScrollTrigger } = useNuxtApp()

 

Does anyone have a idea what is causing the error?

 

 
Link to comment
Share on other sites

It's very difficult to troubleshoot without a minimal demo (like a Stackblitz), but I'd recommend changing your imports - see if either of these works: 

import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'

-OR-

// UMD
import gsap from 'gsap/dist/gsap'
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'

 

Link to comment
Share on other sites

  • Solution

Hi,

 

Apparently this is related to SSR actually. This seems to work the way you intend:

import gsap from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

if (typeof window !== 'undefined') {
  gsap.registerPlugin(ScrollTrigger);
}

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(gsap);
  nuxtApp.provide('gsap', gsap);
  nuxtApp.provide('ScrollTrigger', ScrollTrigger);
});

Hopefully this helps.

Happy Tweening!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...