Jump to content
Search Community

Errors when trying to use Draggable plugin with Next.js / React

KerveCreative test
Moderator Tag

Go to solution Solved by KerveCreative,

Recommended Posts

I am trying to use GSAP Draggable in a Next.js project (i.e. React). Unfortunately I am encountering some errors.

 

I have followed the steps on the installation page, installing gsap using

npm install gsap

 

In my component, I then began with:

import { gsap } from 'gsap';
import { Draggable } from 'gsap/Draggable';
gsap.registerPlugin(Draggable);

But this gave the error:

"SyntaxError: Cannot use import statement outside a module"

 

I read in these forums that this might be fixable by changing the Draggable import to reference the /dist folder:

import { Draggable } from 'gsap/dist/Draggable';

 

But now I get the error:

"TypeError: Cannot read properties of undefined (reading 'style')"

 

As you can see in this screenshot, this appears to be happening in the gsap.registerPlugin part.

 

1365898760_Screenshot2022-12-05at14_51_58.thumb.png.f03d54532f6a54038b50903d9dda9221.png

 

Unfortunately I haven't been able to find a reason why this is happening, or any possible solutions.

 

Is anyone able to suggest why this may be happening, and how I might fix this? Thanks for any help!

Link to comment
Share on other sites

  • Solution

In typical fashion, after posting I've now resolved this issue with some more searching.

 

I had tried removing the following registerPlugin line altogether:

gsap.registerPlugin(Draggable);

 

But that gave another error:

TypeError: _toArray is not a function

 

So I knew this was needed.

 

It turns out that adding a condition to check for the window object around this line solves the issue.

 

The following now works fine:

import { gsap } from 'gsap';
import { Draggable } from 'gsap/dist/Draggable';
if (typeof window !== 'undefined') {
  gsap.registerPlugin(Draggable);
}
  • Like 3
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...