2
votes

I'm trying to import a .json file into my Angular8 project, but for some reason I keep getting the 'Module has no exported member' error.

I tried to simplify the code, but no result this is what I have.

First I added this in the compilerOptions object (tsconfig.json) "resolveJsonModule": true, "esModuleInterop": true,

// assests/example.json
{
  "name": "Spaghetti",
  "desc": "Vegetarische spaghetti bolgnese",
  "rating": 3.7
}

// recipe component
import { example } from '../../../assets/example.json';

  constructor() {
    console.log(example.name);
  }

I keep getting this error: Module '"@angular/recipe-app/src/assets/example"' has no exported member 'example'.ts(2305)

What am I doing wrong?

2

2 Answers

5
votes

You have to use the import without the brackets around the example. You can use one of the following examples:

import example from "../../../assets/example.json";

OR

import * as example from "../../../assets/example.json";
0
votes

You should have something like this

import * as something from '../../../assets/example.json';

and then you can refer something in your .ts file