5

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?

0

2 Answers 2

10

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";
Sign up to request clarification or add additional context in comments.

3 Comments

I've added both (angular 11) and I'm still getting error: "compilerOptions": { "resolveJsonModule": true, "esModuleInterop": true, "outDir": "./out-tsc/app", "types": [], "paths": { "@angular/*": [ "./node_modules/@angular/*" ] } },
@olNoy did you set "resolveJsonModule": true in the tsconfig.json file?
Yes. I'm using webstorm. It might be related to the IDE as I was able to build and run the angular application
0

You should have something like this

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

and then you can refer something in your .ts file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.