4

I've created a vue.js with typescript app using vue-cli 3 and selecting the typescript option.

Now I'm trying to import a .json file:

import * as config from './config.json';

But keep getting the compiler error:

Version: typescript 
2.7.17:25, tslint 5.9.1 
Cannot find module './config.json'.

The config.json file is right next to the .ts that is trying to load it.

Is there any additional config I need to add to load .json with the vue-cli templates?

2 Answers 2

6

Add "resolveJsonModule": true to your tsconfig.json

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

Comments

5

There are a few ways to solve this. One would be to use require() instead of import for non-js files:

const config = require('./config.json')

You can also create a .d.ts file with a wildcard module declaration (under "Wildcard module declarations") which will allow you to import JS files. You can put it anywhere, including it the same as you would any .ts file.

// json-module.d.ts
declare module '*.json' {
  const data: any
  export default data
}

// since we use a default export, we now import it like this
import config from './config.json'

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.