Home > Net >  How to import a JSON file from public directory with Vite?
How to import a JSON file from public directory with Vite?

Time:11-08

I have a Vue3/Vite project where some data has to be read from an external JSON file.

but when i build the project - the JSON file gets bundled.

I need to keep the JSON file external.

What I've tried:

  1. first try vite.config.ts
export default defineConfig({
  optimizeDeps:{
    exclude: ['myfile.json'] // then i tried ['**/myfile.json']
  },
})
  1. second try

vite.config.ts

assetsInclude: ['**/*.json'],
assetsInlineLimit: 0,
  1. third try

App.vue

let jsonData = import.meta.glob('/public/assets/myfile.json')

What am I doing wrong - is there a simple way to keep a JSON file external?

CodePudding user response:

Nvm, in Vite you directly use fetch to grab the data rather than import.

This discussion has an enter image description here

file.json being

{
  "name": "bob",
  "age": 29
}

And with the following result

enter image description here

  • Related