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

javascript - Using require in vite - Stack Overflow

programmeradmin0浏览0评论

I try to use import(xxx) in vite to replace require(xxx), but import(xxx) will return a promise(async), how can I write like require(xxx) in vite?

let lang = require(`./${path}.json`)

Code Image

I try to change it to import(`./${path}.json`) but it will return a Promise, so that I can't get the index with file.

I try to use import(xxx) in vite to replace require(xxx), but import(xxx) will return a promise(async), how can I write like require(xxx) in vite?

let lang = require(`./${path}.json`)

Code Image

I try to change it to import(`./${path}.json`) but it will return a Promise, so that I can't get the index with file.

Share Improve this question edited Nov 27, 2022 at 6:25 dy-xiaodong2022 asked Nov 27, 2022 at 6:23 dy-xiaodong2022dy-xiaodong2022 731 gold badge1 silver badge6 bronze badges 2
  • 1 Inside the package.json file add a line "type": "monjs" and see tell me if its working – Developer Nans Commented Nov 27, 2022 at 6:35
  • did yo find final resolution for this ? – CoderButBeginner Commented May 10, 2024 at 10:28
Add a ment  | 

2 Answers 2

Reset to default 6

Top-level await has decent support in browsers and is available in Node as of two years ago, so you can do:

const lang = await import(`./${path}.json`, { assert: { type: "json" } })

This question is solved, just install package vite-require than require is work on vite.

发布评论

评论列表(0)

  1. 暂无评论