Jump to content
Search Community

ScrollSmoother error "_mainInstance.content is not a function" with SvelteKit

felixbaumgaertner test
Moderator Tag

Go to solution Solved by felixbaumgaertner,

Recommended Posts

I've already successfully used gsap core functions in another SvelteKit project, so now I wanted to use plugins for the first time. I registered and upgraded to the ShockinglyGreen license, then followed the installation guide via npm. My code is based on the example code for ScrollSmoother.
 

<script lang="ts">
	import { gsap } from "gsap/dist/gsap";
	import { ScrollSmoother } from "gsap/dist/ScrollSmoother";

	gsap.registerPlugin(ScrollSmoother);
	ScrollSmoother.create({
		smooth: 1,
		effects: true,
		smoothTouch: 0.1
	});
</script>

<div id="smooth-wrapper">
	<main id="smooth-content">
		<slot />
	</main>
</div>

The above code produces the following error when using npm run dev:

 

TypeError: _mainInstance.content is not a function
    at ScrollSmoother.create (/home/fb/projects/fb.de/node_modules/gsap/dist/ScrollSmoother.js:762:51)
    at eval (/src/routes/__layout.svelte:15:39)
    at Object.$$render (/home/fb/projects/fb.de/node_modules/svelte/internal/index.js:1758:22)
    at eval (/.svelte-kit/generated/root.svelte:50:126)
    at $$render (/home/fb/projects/fb.de/node_modules/svelte/internal/index.js:1758:22)
    at Object.render (/home/fb/projects/fb.de/node_modules/svelte/internal/index.js:1766:26)
    at render_response (file:///home/fb/projects/fb.de/.svelte-kit/runtime/server/index.js:1223:27)
    at async respond_with_error (file:///home/fb/projects/fb.de/.svelte-kit/runtime/server/index.js:2678:10)
    at async respond$1 (file:///home/fb/projects/fb.de/.svelte-kit/runtime/server/index.js:2932:4)
    at async resolve (file:///home/fb/projects/fb.de/.svelte-kit/runtime/server/index.js:3304:11)

When I searched for this error I couldn't find anything related. Am I just missing something simple? How can this be fixed?

 

Also I'm not familiar with posting my error on a forum, so if there is more information needed or if this is the wrong place to ask for help I'm happy about any information.

Link to comment
Share on other sites

First of all, welcome to Club GreenSock

 

That error sounds quite strange actually. It's difficult to troubleshoot without a minimal demo. Could you provide one please so that we can reproduce the error on our end? It almost sounds like somewhere in your code you've got ScrollSmoother() called (as a function). I'm also curious if you tried loading and registering ScrollTrigger because that's a dependency of ScrollSmoother. In other words, you should gsap.registerPlugin(ScrollTrigger, ScrollSmoother). 

 

If that doesn't help, definitely post a minimal demo and we'll take a peek. You're welcome to use CodeSandbox for the minimal demo. Here's a starter with Svelte: https://codesandbox.io/s/gsap-svelte-starter-cbvtv

 

The simpler the better - please do NOT post your whole project. A few colored <div> elements with the necessary ScrollSmoother code is perfect.

Link to comment
Share on other sites

Thanks for the quick response! I've reproduced it based on the codesandbox starter you provided: https://codesandbox.io/s/sveltekit-gsap-scrollsmoother-minimal-demo-iu3o60?file=/src/routes/__layout.svelte

For me the error doesn't always show up (I'm new to codesandbox.io), so if you run yarn start in a new terminal and refresh the embedded browser it should work.

Whilst debugging this I've also found the following error thrown right before the first mentioned error ("_mainInstance.content is not a function") appeared:

Cannot read property 'utils' of undefined
TypeError: Cannot read property 'utils' of undefined
    at _getTarget (/sandbox/node_modules/gsap/dist/ScrollTrigger.js:138:17)
    at Function.ScrollTrigger$1.getScrollFunc (/sandbox/node_modules/gsap/dist/ScrollTrigger.js:2524:27)
    at new ScrollSmoother (/sandbox/node_modules/gsap/dist/ScrollSmoother.js:118:38)
    at Function.ScrollSmoother.create (/sandbox/node_modules/gsap/dist/ScrollSmoother.js:762:109)
    at __layout.svelte:16:16
    at Object.$$render (/sandbox/node_modules/svelte/internal/index.js:1745:22)
    at root.svelte:37:37
    at $$render (/sandbox/node_modules/svelte/internal/index.js:1745:22)
    at Object.render (/sandbox/node_modules/svelte/internal/index.js:1753:26)
    at render_response (file:///sandbox/.svelte-kit/runtime/server/index.js:1223:27)

 

Also I removed my module installation token from the .npmrc after installation, just to hide it from public.

Link to comment
Share on other sites

I'm still pretty lost I'm afraid. Your demo's still using the old GSAP imports.

I've changed your imports in this one but with your setup I can't even get GSAP to work properly, let alone the plugins. I'm not quite sure how to move forward as I don't really know svelte or sveltekit.

 

Maybe this thread will help?
 


or this one?
 

 

  • Like 1
Link to comment
Share on other sites

Thanks for you effort Cassie, I really appreciate it! In your pen you try to import from "gsap-trial/dist/gsap", but the installed module is at "gsap/dist/gsap" - just fyi.

I will look into your linked posts, but I think I've already read them before opening my own post. Maybe it's the way I try to use it so I will try relying on "onMount" next.

  • Like 1
Link to comment
Share on other sites

You switched it over to the gsap-trial package though so it shouldn't be using that GSAP module anymore

1 hour ago, felixbaumgaertner said:

Hey Cassie, thanks for your reply! I've switched to the gsap-trial package, the pen now works without bad gateway bugs. I also ran the pen successfully on another device to confirm it is working.


If you're still using the normal GSAP package on codesandbox then scrollSmoother won't work because it's a club plugin. It's only available through gsap-trial on codesandbox

Link to comment
Share on other sites

You are right Cassie I had to remove and install gsap-trial again to make it work correctly. Now really using gsap-trial, that mistake was on my end. So now the error changed to:

TypeError: U.content is not a function
    at Function.t.create (/sandbox/node_modules/gsap-trial/dist/ScrollSmoother.js:13:14207)
    at eval (/src/routes/__layout.svelte:18:39)
    at Object.$$render (/sandbox/node_modules/svelte/internal/index.js:1745:22)
    at eval (/.svelte-kit/generated/root.svelte:50:126)
    at $$render (/sandbox/node_modules/svelte/internal/index.js:1745:22)
    at Object.render (/sandbox/node_modules/svelte/internal/index.js:1753:26)
    at render_response (file:///sandbox/.svelte-kit/runtime/server/index.js:1223:27)
    at async respond_with_error (file:///sandbox/.svelte-kit/runtime/server/index.js:2680:10)
    at async respond$1 (file:///sandbox/.svelte-kit/runtime/server/index.js:2934:4)
    at async resolve (file:///sandbox/.svelte-kit/runtime/server/index.js:3306:11)
  • Like 1
Link to comment
Share on other sites

Lets just step back a bit and ignore scrollSmoother for now - You say you had to remove and install to make it work 'correctly'

Can you get a GSAP tween working with the GSAP-trial package and your setup? Because I can't get your demo working even with the core GSAP-trial package 

 

If you can could you give me a link to the working demo?
 

Link to comment
Share on other sites

  • Solution

Okay I got it working in my main project now. The solution was to use onMount(), which I thought was optional, and import + register ScrollTrigger as you already suggested because it works as a dependency. I've updated the code I posted here to the working version:

 

<script lang="ts">
  import { onMount } from "svelte";
  import { gsap } from "gsap/dist/gsap";
  import { ScrollTrigger } from "gsap/dist/ScrollTrigger";
  import { ScrollSmoother } from "gsap/dist/ScrollSmoother";
  gsap.registerPlugin(ScrollTrigger, ScrollSmoother);

  onMount(() => {
    ScrollSmoother.create({
      smooth: 1,
      effects: true,
      smoothTouch: 0.1
    });
  });
</script>

<div id="smooth-wrapper">
  <main id="smooth-content">
    <slot />
  </main>
</div>

 

The error message threw me way too much off, thank you guys very much!

  • Like 1
Link to comment
Share on other sites

  • 11 months later...

I am having the same error come up, but I am using a Remix project:

 

import { gsap } from 'gsap/dist/gsap'
import { ScrollSmoother } from 'gsap/dist/ScrollSmoother'

...

useEffect(() => {
    gsap.registerPlugin(ScrollSmoother)
    scrollSmoother.current = ScrollSmoother.create({
      smooth: 1,
      effects: true,
      content: '#smooth-content'
    })
  }, [])

I get this error:

TypeError: _mainInstance.content is not a function
    at ScrollSmoother.create (http://localhost:3000/build/_shared/chunk-2KGHTWMG.js:4566:55)
    at http://localhost:3000/build/routes/($role)-V6X7IM6L.js:5769:67
    at commitHookEffectListMount (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:16436:34)
    at invokePassiveEffectMountInDEV (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:17845:19)
    at invokeEffectsInDev (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:19194:19)
    at commitDoubleInvokeEffectsInDEV (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:19179:15)
    at flushPassiveEffectsImpl (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:18996:13)
    at flushPassiveEffects (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:18940:22)
    at http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:18821:17
    at workLoop (http://localhost:3000/build/_shared/chunk-VKRUHKJ5.js:195:42)

Any insights?

  • Like 1
Link to comment
Share on other sites

Hi @IvyFu. It's difficult to troubleshoot without a minimal demo, but that error seems extremely odd to me and the only thing I can think of that might cause that is if you called the ScrollSmoother constructor in such a way that it used a non-standard scope. We'd really need to see a minimal demo to know what's going on for sure though. 

 

Do you have something like this somewhere in your code?:

// BAD!!!
ScrollSmoother({...});
  
// GOOD
ScrollSmoother.create({...});

 

I'd also recommend registering ScrollTrigger and ScrollSmoother. 

gsap.registerPlugin(ScrollTrigger, ScrollSmoother);

(I doubt that has anything to do with the error, though)

 

Here's a starter Stackblitz that you can fork to recreate your issue: 

https://stackblitz.com/edit/react-cxv92j

Link to comment
Share on other sites

@IvyFu did you figure out the issue with SmoothScroller and Remix? I've just run into the same error I believe.
 

TypeError: _mainInstance.content is not a function
    at ScrollSmoother.create (http://localhost:3000/build/routes/index-4MOFVTFL.js:8326:49)
    at http://localhost:3000/build/routes/index-4MOFVTFL.js:8363:20
    at commitHookEffectListMount (http://localhost:3000/build/entry.client-MDLAOY3P.js:16443:34)
    at commitPassiveMountOnFiber (http://localhost:3000/build/entry.client-MDLAOY3P.js:17684:19)
    at commitPassiveMountEffects_complete (http://localhost:3000/build/entry.client-MDLAOY3P.js:17657:17)
    at commitPassiveMountEffects_begin (http://localhost:3000/build/entry.client-MDLAOY3P.js:17647:15)
    at commitPassiveMountEffects (http://localhost:3000/build/entry.client-MDLAOY3P.js:17637:11)
    at flushPassiveEffectsImpl (http://localhost:3000/build/entry.client-MDLAOY3P.js:18990:11)
    at flushPassiveEffects (http://localhost:3000/build/entry.client-MDLAOY3P.js:18947:22)
    at http://localhost:3000/build/entry.client-MDLAOY3P.js:18828:17
Link to comment
Share on other sites

That just means that your build process doesn't understand ES Modules, so you can just switch to importing the UMD files in the /dist/ directory as stated earlier in this thread: 

 

// old (ESM)
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

// new (UMD)
import { gsap } from 'gsap/dist/gsap';
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger';

 I assume you wanted something more like this?: 

https://codesandbox.io/p/sandbox/upbeat-dan-1lttbi?file=%2Fapp%2Fcomponents%2Ftest.tsx

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...