最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue 3 "Uncaught ReferenceError: require is not defined" - Stack Overflow

programmeradmin3浏览0评论

I am working on my first Vue 3 project after using Vue 2 for a long time.

Normally in my ponents on Vue 2 projects I can import a library, for example moment, like so:

<script>
const moment = require("moment")
export default {
  ...
</script>

In vue 3, I'm trying to do the same:

<script setup>
const moment = require("moment")
</script>

However I get the error

Uncaught ReferenceError: require is not defined

How can I get around this and import modules into a vue3 ponent?

I am working on my first Vue 3 project after using Vue 2 for a long time.

Normally in my ponents on Vue 2 projects I can import a library, for example moment, like so:

<script>
const moment = require("moment")
export default {
  ...
</script>

In vue 3, I'm trying to do the same:

<script setup>
const moment = require("moment")
</script>

However I get the error

Uncaught ReferenceError: require is not defined

How can I get around this and import modules into a vue3 ponent?

Share Improve this question edited Jul 1, 2022 at 13:08 Nikola Pavicevic 23.5k9 gold badges29 silver badges51 bronze badges asked Jun 29, 2022 at 15:57 corycorycorycorycorycory 1,6563 gold badges25 silver badges50 bronze badges 1
  • even on vue2, you should use import not require – Lk77 Commented Jul 1, 2022 at 13:13
Add a ment  | 

1 Answer 1

Reset to default 3

You cann't use require in Vite, did you try with import:

<script setup>
  import moment from 'moment'
发布评论

评论列表(0)

  1. 暂无评论