Kingamere
Kingamere

Reputation: 10156

Importing json file in typescript 2.9.2 and Angular 6 app is not working

I am running an Angular 6 app, with this version of typescript: "typescript": "~2.9.2"

I followed this answer to import json files in typescript 2.9.*, but it's not working.

Here is my tsconfig.json file:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

And then my app.component.ts file where I'm importing it:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit, OnDestroy {

But I'm getting the error:

error TS2497: Module '"C:/Users/..../config"' resolves to a non-module entity and cannot be imported using this construct.

Upvotes: 2

Views: 1448

Answers (1)

bluebit
bluebit

Reputation: 83

I used "allowSyntheticDefaultImports": true to make this work and import version from './package.json'. According to the docs synthetic imports allow default imports from modules with no default export. This does not affect code emit, just typechecking. Using this, I no longer had the error you mentioned.

Upvotes: 4

Related Questions