0

I'm trying to import some json files that are within sub-folders of a library inside node_modules, and typescript is saying it can't find the sub-folder.

The library is cldr-data, and the specific path I need is cldr-data/supplemental/likelySubtags.json.

browser location

I've tried the following imports:

import likelySubtags from 'cldr-data/supplemental/likelySubtags.json';
import likelySubtags from './../../node_modules/cldr-data/supplemental/likelySubtags.json';

but both give the error: [ts] Cannot find module. The strange thing is it can find cldr-data, it just can't find the sub-folder supplemental. I'm not sure how to make it find supplemental/.

user1779418
  • 455
  • 1
  • 7
  • 22

1 Answers1

0

You need to prepare the application with additional flags in your tsconfig.json beforehand. You should add additional compiler options:

ResolveJSONModule

--resolveJsonModule allows for importing, extracting types from .json files.

ESModuleInterop

--esModuleInterop Allows default imports from modules with no default export. This is required since a .json file has no default output.

"resolveJsonModule": true,
 "esModuleInterop": true

Now in component you can use :

import likelySubtags from 'cldr-data/supplemental/likelySubtags.json';
nircraft
  • 8,242
  • 5
  • 30
  • 46
  • Unfortunately this doesn't fix my problem. It can't find the supplemental folder at all: Cannot find module 'cldr-data/supplemental/' – user1779418 Oct 25 '19 at 19:17