In my Angular application, I'd like to display the version from the package.json file.
I know that to import it I need to allow importing json and for this tsconfig.json file needs to get a few extra lines:
{
"compilerOptions": {
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node"
}
}
I did this, but after that when I import json file in my component class:
import { myPackage } from '../../../package.json';
I still get an error:
Cannot find module '../../../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.ts(2732)
And problem is not with path as I put another simple json file to the same folder but getting the same error.
I've googled and seen many examples that speak about '--resolveJsonModule', but it seems like I do all what's needed... but it still does not work.
I also restarted VScode and angular service - it did not help.
I use Angular 10.0.11 and TS 3.9.7
Please advise! Thanks!
Deepak posted an almost proper answer, but for some reasons it did not work to me. Though, referenced answer (https://stackoverflow.com/a/48869478/6904274) has comments that actually helped.
In my case, the Angular application had more than just tsconfig.json, and to be able to import JSON files I had to modify other 'flavours': base, spec, and app. And each is responsible for their own thing. Obviously, to have that import work in components under tests, I need to add it to 'spec'. But, better to just 'base'.