【问题标题】:Vue-konva running into error: Must use import to load ES ModuleVue-konva 遇到错误:必须使用 import 来加载 ES 模块
【发布时间】:2021-12-17 01:13:24
【问题描述】:

我正在尝试按照documentation here 在我的应用程序中实现Vue-konva。但是我遇到了以下错误:

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.

我做了以下步骤:

  1. 安装npm install vue-konva konva --savenpm install canvas --save
  2. 在我的Vue 应用程序中添加了来自documentation 的代码。
  3. 重新启动应用程序时出现错误。

以下是我尝试解决的方法:

  1. 我正在使用node version v14.16.0
  2. 我在package.json 文件中添加了以下内容:
"ssr": false,
"type":"module",
  1. 删除node-modules 文件夹并使用npm-install 重新创建。

我觉得我正在做文档中提到的所有事情,但仍然不确定为什么会出现错误。有人可以帮我解决这个问题吗?

这是应用程序的完整代码:

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

** 更新 ** 我试着喜欢这个并得到了错误,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>

【问题讨论】:

  • 不确定 "ssr": false, 是什么,但您是否尝试将 Nuxt 插件设置为 mode: 'client'nuxtjs.org/docs/configuration-glossary/…
  • 如果你想在本地使用它,你可以把它包装成一个&lt;client-only&gt;&lt;/client-only&gt;或者只在客户端加载它:stackoverflow.com/a/69572014/8816585(如果这个特定的包不支持SSR)跨度>
  • 另外,你试过这个吗? github.com/konvajs/vue-konva/issues/9
  • @kissu 非常感谢您的回复。 SSR 是我只是想看看它是否可行。在其中一个答案中提到了它,所以我添加了它。我应该在nuxt.config 中添加mode 对,但我没有使用CDN 而不是npm package,所以我没有添加mode:client。我也尝试添加它,但它对我不起作用。我什至尝试添加 &lt;client-only&gt; 也不起作用。
  • 我的意思是,这个是关于 Konva 的,所以是的,看起来是合法的。

标签: javascript node.js vue.js nuxt.js konvajs


【解决方案1】:

这个应该可以的

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

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 2020-11-10
    • 2021-12-01
    • 2021-06-18
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2021-10-30
    • 2022-11-11
    相关资源
    最近更新 更多