2

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.
4
  • I'm not familiar with Angular, but did you try import config from '../../config.json'; ? Commented Apr 9, 2019 at 20:42
  • @Striped Now I'm getting: ...has no default export Commented Apr 9, 2019 at 20:42
  • I see this, could it help ? angularjswiki.com/angular/… Commented Apr 9, 2019 at 20:45
  • There is a diffenrence, the "esModuleInterop": true within the tsconfig.json file which allow default imports from modules with no default export. Commented Apr 9, 2019 at 20:47

1 Answer 1

4

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.

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

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.