15

I just created a clean VueJS new project and I can't get loading a JSON file to work.

Reproduction / Issue

For easy reproduction I've created a Github repo with my issue: https://github.com/devedse/VueJSImportJsonFile

In the Home.vue page I'm trying to get Json loading to work. (https://github.com/devedse/VueJSImportJsonFile/blob/master/src/views/Home.vue)

When you open this solution in VSCode the following line shows an error:

import theJson from '@/assets/hi.json';

Error in VSCode Can't find module '@/assets/hi.json'

When running NPM Serve the following error pops up: enter image description here

What I've already tried

I've already searched all of stackoverflow and tried everything in the following posts:

Importing json file in TypeScript

Typescript compiler error when importing json file

https://github.com/chybie/ts-json

Edit 1:

Ok I now managed to get it to work when running npm run serve by adding this to the tsconfig.json:

{
  "compilerOptions": {
    "resolveJsonModule": true
  }
}

However the error in VSCode seems to stay. Is there a way to fix this too?

0

2 Answers 2

21

Simply add resolveJsonModule": true to your tsconfig.json under compilerOptions:

diff --git a/tsconfig.json b/tsconfig.json
index 499f5e2..a05dab1 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -6,6 +6,7 @@
     "jsx": "preserve",
     "importHelpers": true,
     "moduleResolution": "node",
+    "resolveJsonModule": true,
     "esModuleInterop": true,
     "allowSyntheticDefaultImports": true,
     "sourceMap": true,
Sign up to request clarification or add additional context in comments.

Comments

0

In your tsconfig.app.json config file add "src/**/*.json" in the list of patterns for the files to be included in compilation so it looks like this:

{
  "include": [
    "env.d.ts",
    "src/**/*",
    "src/**/*.vue",
    "src/**/*.json" <----- this
  ]
}

Source: https://www.typescriptlang.org/tsconfig#include

Comments

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.