2

I'm migrating from Vue 2 to Vue 3. Doing test app first.

I'm having trouble importing a JSON file.

This error I'm getting

The file exists. Tried all these variants:

import { sites } from "./data/sites.json";
import sites from "./data/sites.json";
import sites from "../data/sites.json";
import sites from "@/data/sites.json";

https://www.koderhq.com/tutorial/vue/local-json/ and https://codesandbox.io/s/z2mpz6zq23?file=/src/components/HelloWorld.vue

Sandbox Link - dev - This doesn't work

Thanks @Tolbxela I tried that: enter image description here

0

2 Answers 2

1

Your Codesandbox link doesn't run, so I recreated it in StackBlitz.

Your directory layout looks like this:

src
├── components
│   └── GoodTable.vue
├── data
│   └── sites.json

Notice import rows from './data/sites.json' in src/components/GoodTable.vue. ./data/sites.json is a relative path, so the lookup is relative to the importing file's directory, which is src/components. src/components/data/sites.json does not exist, so Vite displays an error.

The JSON file is actually one parent level up in src/data/sites.json, so you need to update the import path to:

import rows from '../data/sites.json'

demo

Sign up to request clarification or add additional context in comments.

Comments

0

Your app in the sandbox is working well. Check the screenshot.

I would guess the problem is with your browser. Be sure it supports the import statement (MDN).

it works

UPDATE:

I've checked your screenshot. You have an import problem in the Vite (plugin:vite:import-analysis)

Check these links to solve:

Vite - Dynamic Import

StackOverflow - plugin:vite:import-analysis - Failed to parse source for import analysis because the content contains invalid JS syntax. - Vue 3

7 Comments

Links aren't mine.
Migrated to JavaScript same problem.
The problem is Vite- Did test project with vue-cli (which uses Webpack) works fine.
Done Vite Test project: If I import JSON App.vue it works: import data from './data/sites.json'. If I try load anywhere else, for example HeeloWorld.vue problem as above.
@Anthony, I have no experience with Vite unfortunately. So, it hard to help. The Docs could help you. Take a look at the glob import: vitejs.dev/guide/features.html#glob-import. It looks like the way you should investigate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.