1

In my Ionic2 project I imported a JSON file with this statement:

import * as data from './data.json';

I also created the file typings.d.ts in the project's root directory with this contents:

declare module "*.json" {
    const value: any;
    export default value;
}

As suggested here. But this does not work as intended, the module still cannot be found. Downloading the sample project from this tutorial and running it works fine. though, I cannot spot any difference between this project and my own, besides that my files reside in /project/src/pages/page.ts.

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
herhuf
  • 497
  • 3
  • 17
  • Possible duplicate of [Importing json file in TypeScript](https://stackoverflow.com/questions/49996456/importing-json-file-in-typescript) – c69 Jan 07 '19 at 07:27

1 Answers1

0

You can try the following:

interface SomeJson {
    // ... 
}

const json: SomeJson = require("./data.json");
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
  • How can I use `require()`? I installed requirejs but the function is still not available. – herhuf Jan 16 '18 at 16:06
  • You shouldn't need to install require. The require is supported natively, just make sure that you have one of the latest versions of TypeScript. If you are using Webpack it should work. – Remo H. Jansen Jan 17 '18 at 08:39
  • That's strange, everything is up to date, yet visual studio code is telling me the name `require` cannot be found. – herhuf Jan 17 '18 at 09:39
  • Can you please add your `tsconfig.json` to the question? – Remo H. Jansen Jan 17 '18 at 11:33