简体   繁体   English

Vue-konva 运行出错:必须使用导入来加载 ES 模块

[英]Vue-konva running into error: Must use import to load ES Module

I am trying to implement the Vue-konva into my application by following the documentation here .我正在尝试按照此处文档Vue-konva到我的应用程序中。 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 .安装npm install vue-konva konva --savenpm install canvas --save
  2. Added the code in my Vue app from the documentation .文档中添加了我的Vue应用程序中的代码。
  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我正在使用node version v14.16.0
  2. I added the following in my package.json file:我在package.json文件中添加了以下内容:
"ssr": false,
"type":"module",
  1. Remove node-modules folder and create again with npm-install .删除node-modules文件夹并使用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 **更新** 我试图喜欢这个并得到错误, 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>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM