There is an easy reason for problems like this in Next.js.
Next.js is an SSR library and GSAP is a Client Side library. When You import GSAP in the top of your project, it throws an error because GSAP can't access to Window object in JS.
But, when you require it in your componentDidMount(), it will require after the page loaded and it will access to Window object in js.
Hope it will help you all.
The new way to get around this problem is to use dynamic component with disabled SSR for the component that's using GSAP https://nextjs.org/docs#with-no-ssr
React, Next.js and GSAP issues - possible fixes for SSR
in GSAP
Posted
The new way to get around this problem is to use dynamic component with disabled SSR for the component that's using GSAP https://nextjs.org/docs#with-no-ssr