3

I am trying to implement the Vue-konva into my application by following the documentation here. But I am running into the following error:

Must use import to load ES Module: /Users/myName/projects/projectName/node_modules/konva/lib/index-node.js require() of ES modules is not supported. 
require() of /Users/myName/projects/projectName/node_modules/konva/lib/index-node.js from /Users/myName/projects/projectName/node_modules/vue-konva/umd/vue-konva.js is an ES module file as it is a .js file whose nearest parent package.json 
contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index-node.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/myName/projects/projectName/node_modules/konva/package.json.

I did the following steps:

  1. Install npm install vue-konva konva --save and npm install canvas --save.
  2. Added the code in my Vue app from the documentation.
  3. When I restart the application I get the error.

Following things I tried for work-around:

  1. I am using node version v14.16.0
  2. I added the following in my package.json file:
"ssr": false,
"type":"module",
  1. Remove node-modules folder and create again with npm-install.

I feel like I am doing everything that has been mentioned in the documentation but still not sure why I am getting the error. Can someone please help me with this issue?

Here is the complete code from the application:

<template>
  <v-stage :config="configKonva">
    <v-layer>
      <v-circle :config="configCircle" />
    </v-layer>
  </v-stage>
</template>

<script>
import Vue from 'vue'
import VueKonva from 'vue-konva'

Vue.use(VueKonva)

export default {
  data () {
    return {
      configKonva: {
        width: 200,
        height: 200
      },
      configCircle: {
        x: 100,
        y: 100,
        radius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4
      }
    }
  }
}
</script>

<style>
body {
  margin: 0;
  padding: 0;
}
</style>

** UPDATED ** I tried to like this and got the error, client.js:227 TypeError: Vue.use is not a function

<template>
  <v-stage :config="configKonva">
    <v-layer>
      <v-circle :config="configCircle" />
    </v-layer>
  </v-stage>
</template>

<script>
export default {
  data () {
    return {
      configKonva: {
        width: 200,
        height: 200
      },
      configCircle: {
        x: 100,
        y: 100,
        radius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4
      }
    }
  },
  async mounted () {
    if (process.browser) {
      const Vue = await import('vue')
      const VueKonva = await import("vue-konva")
      Vue.use(VueKonva)
      console.log('HELLO FROM MOUNTED')
    }
  }
}
</script>
kissu
  • 40,416
  • 14
  • 65
  • 133
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • Not sure what is `"ssr": false,` but did you tried setting up the Nuxt plugin as `mode: 'client'`? https://nuxtjs.org/docs/configuration-glossary/configuration-plugins#the-plugins-property – kissu Nov 02 '21 at 15:04
  • If you want to use it locally, you could maybe wrap it into a `` or maybe load it only on the client: https://stackoverflow.com/a/69572014/8816585 (if this specific package does not support SSR) – kissu Nov 02 '21 at 15:05
  • Also, did you tried this one? https://github.com/konvajs/vue-konva/issues/9 – kissu Nov 02 '21 at 15:16
  • @kissu Thanks a lot for your response. `SSR` is something I was just trying to see if it would work. In one of the answers, it was mentioned so I added it. I should add `mode` in `nuxt.config` right but I am not using the `CDN` rather the `npm package` so I have not added `mode:client`. I tried adding this as well but it's not working for me. I even tried adding `` that's also not working. – BATMAN_2008 Nov 02 '21 at 15:18
  • `ssr: false` is for `nuxt.config.js` actually. If you want to have an SPA only Nuxt project. Not sure what is `mode` or even `mode:client` for you. And yeah, I'm not talking about the CDN. Your issue seems to be connected to this question btw: https://github.com/konvajs/vue-konva/issues/163 Maybe give it a bump! – kissu Nov 02 '21 at 15:21
  • @kissu Thanks a lot for the response. I have added an comment within it. I am not understanding why I am getting this issue but this I get only when I use the `Vue-Konva`. Normally, I do not get this. – BATMAN_2008 Nov 02 '21 at 15:29
  • 1
    I mean, this one is about Konva so yeah, seems legit. – kissu Nov 02 '21 at 15:31
  • @kissu Still unable to find the resolution. I am still getting the error. Any help would be really appreiciated. – BATMAN_2008 Nov 03 '21 at 21:23
  • Did you tried the same as yesterday (with browser-ui) with this package? It may help taking the same approach IMO. – kissu Nov 04 '21 at 04:53
  • @kissu I tried even that but it did not work and ran into various other issues. I have updated my code. I am trying out a few libraries to see which will suffice my need but I am unable to configure them. Looking forward to your response. – BATMAN_2008 Nov 04 '21 at 06:59

1 Answers1

1

This one should work fine

<script>
import Vue from 'vue'

export default {
  async mounted () {
    if (process.browser) {
      const VueKonva = await import('vue-konva')
      Vue.use(VueKonva)
      console.log('HELLO FROM MOUNTED')
    }
  }
}
</script>
kissu
  • 40,416
  • 14
  • 65
  • 133