dy-xiaodong2022
dy-xiaodong2022

Reputation: 73

Using require in vite

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.

Upvotes: 7

Views: 15643

Answers (2)

dy-xiaodong2022
dy-xiaodong2022

Reputation: 73

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

Upvotes: 0

Zac Anger
Zac Anger

Reputation: 7787

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" } })

Upvotes: 6

Related Questions