GSAP ScrollTrigger with Next.js
Asked Answered
C

2

8

I'm trying to use ScrollTrigger with Next.js:

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

gsap.registerPlugin(ScrollTrigger);

I got this error: enter image description here

Does anyone have a solution for this problem?

Crafton answered 12/1, 2021 at 17:20 Comment(0)
I
16

UMD option

You can either load the UMD version (located under the dist/ subdirectory).

import { gsap } from "gsap/dist/gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";

ESM option

OR use the default ESM format and transpile gsap library in Next.js.

To do so, you'll first need to install next-transpile-modules.

$ npm install next-transpile-modules

Then some extra setup in your next.config.js file is required.

// next.config.js
const withTM = require("next-transpile-modules")(["gsap"]);

module.exports = withTM({});

You'll be then able to import it the way you currently are.

import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
Illicit answered 12/1, 2021 at 17:52 Comment(0)
M
0

This will work ->

import { gsap } from "gsap/dist/gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";

Myeshamyhre answered 29/6, 2023 at 5:42 Comment(1)
Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful. - From ReviewIllicit

© 2022 - 2024 — McMap. All rights reserved.