Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

javascript - ScrollTrigger with Next js

im 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...