Consider Using '--resolvejsonmodule' To Import Module With '.json' Extension Code Example


Example 1: Cannot find module './data.json'. Consider using '--resolveJsonModule' to import module with '.json' extension


add to tsconfig.json

{
...
"resolveJsonModule": true
}
}

Example 2: Consider using '--resolveJsonModule


add "resolveJsonModule": true key in tsconfig.json

{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true
}
}

Example 3: read json file in typescript


// data.json:
{
"greeting" : "xin chao Vietnam"
}
// component:
import * as data from 'data.json';
let greeting = data.greeting;
//registering jsonModule in tsconfig.json
"compilerOptions": {
....
"resolveJsonModule": true,
....
},

Example 4: how to import a json string from a file in typescript


import {default as a} from "a.json";
a.primaryMain

Example 5: --resolveJsonModule


declare module "*.json" {
const value: any;
export default value;
}

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools