0

I am using angular4, in a simple component I am trying to load a json, the json and component are on the same path/folder.

import metacoin_artifacts from './MetaCoin.json';

I also tried

import metacoin_artifacts from 'MetaCoin.json';

all of them throw me a error Cannot find module './MetaCoin.json'.

The first approach used to work on one project before, but now it can't work anymore, I don't know what is the difference between them.

user824624
  • 7,077
  • 27
  • 106
  • 183

1 Answers1

1

Json is not a module, you cannot load json file as above, you can do it as,

 public getJSON(): Observable<any> {
         return this.http.get("./MetaCoin.json")
                         .map((res:any) => res.json())
                         .catch((error:any) => console.log(error));

 }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • my json file is at the project root, the same path with the src folder. calling the this.http.get('./MetaCoin.json') returns me a GET http://localhost:4200/MetaCoin.json – user824624 Nov 22 '17 at 01:45
  • look at the answer here https://stackoverflow.com/questions/43275995/angular4-how-do-access-local-json – Sajeetharan Nov 22 '17 at 01:50