3

I want to use scrollmagic with nuxtjs.

I installed scrollmagic via npm.

npm install scrollmagic

In my nuxt.config.js file i added

build: {
    vendor: ['scrollmagic']
},

And in my pages/index.vue file i simply imported it.

import ScrollMagic from 'scrollmagic'

But this results only in this error

[vue-router] Failed to resolve async component default: ReferenceError: window is not defined [vue-router] uncaught error during route navigation: ReferenceError: window is not defined at C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:37:2 at C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:22:20 at Object. (C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:27:2)

How can i fix this?

Gregor Voinov
  • 2,203
  • 7
  • 34
  • 52

1 Answers1

12

Add a file to your plugins folder called "scrollmagic.js" and paste the following code into it:

plugins/scrollmagic.js

import ScrollMagic from 'scrollmagic'

Add the plugin to your nuxt.config.js file

nuxt.config.js

module.exports = {
  build: {
    vendor: ['scrollmagic']
  },
  plugins: [
    // ssr: false to only include it on client-side
    { src: '~/plugins/scrollmagic.js', ssr: false }
  ]
}

Use it with if (process.client) {}

page or component

<script>
let scrollmagic
if (process.client) {
  scrollmagic = require('scrollmagic')
// use scrollmagic
}
</script>

For more information please consult the excellent documentation on this topic: https://nuxtjs.org/guide/plugins/

Aldarund
  • 17,312
  • 5
  • 73
  • 104
Schwesi
  • 4,753
  • 8
  • 37
  • 62
  • This solved the issue about "window is not defined" but now I'm getting this `ERROR calling setTween() due to missing Plugin 'animation.gsap'. Please make sure to include plugins/animation.gsap.js`. Have yet to find a solution for NuxtJS... Any idea on this? – ZeferiniX Apr 18 '18 at 16:38
  • Did you add a file to your plugins folder called "animation.gsap.js"? If so, are you sure that there are no typos? – Schwesi Apr 19 '18 at 17:34