0
var url = 'assets/json/data.json' //this url changes based on the environment

function() {
const data = require(url);
console.log(data);
}

I am unable to read the json data. It throws the error saying 'app.exceptions.ts:34 Error: Uncaught (in promise): Error: Cannot find module'

Please help.

  • 2
    https://stackoverflow.com/questions/51173263/import-static-json-file-from-assets-dir-in-component-angular – Alaksandar Jesus Gene Apr 06 '21 at 12:06
  • Does this answer your question? [Import static JSON file from assets dir in component (Angular)](https://stackoverflow.com/questions/51173263/import-static-json-file-from-assets-dir-in-component-angular) – Giannis Apr 06 '21 at 13:03
  • This works for static file (fixed path). In my case I am referring from multiple paths. – Venkatesh Prasad Apr 06 '21 at 13:05
  • Perhaps change `url` to `'./assets/json/data.json'` if the file is located relative to the executing script, or `'/assets/json/data.json'` if relative to the host root – msbit Apr 06 '21 at 14:38

0 Answers0