2

I added a file students.json in the folder src/app.

In the file app.component.ts, I have an error message

Cannot find module './students.json'.

Here is the code:

import { Component } from '@angular/core';
import studentsData from './students.json';
  
interface Student {
    id: Number;
    name: String;
    email: String;
    gender: String;
}
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  
  students: Student[] = studentsData;
}

We agree that the patch is correct?

The problem is perhaps in the tsconfig.json ?

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false
  }
}

https://stackblitz.com/

4
  • 1
    I don't think you can import a JSON file... Where did you read that you can do it? Commented Jul 23, 2021 at 10:28
  • @Aviad P. Seriouse? Here ?? itsolutionstuff.com/post/… Commented Jul 23, 2021 at 12:14
  • I was serious but I learned something new. Commented Jul 23, 2021 at 12:15
  • @Aviad P. : Ah ok , Me also... ^^ Commented Jul 23, 2021 at 12:18

1 Answer 1

2

Add "resolveJsonModule": true to your tsconfig.json in compilerOptions section:

  "compilerOptions": {
    ...
    "resolveJsonModule": true
    ...
  }

See the forked stackblitz - https://stackblitz.com/edit/angular-ivy-1rxfdn?file=tsconfig.json

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Michael, why it works on Stackblitz but not on Visual Studi even with "resolveJsonModule": true ? You have an idea?
@christine check this link github.com/microsoft/TypeScript/issues/25400 (restart VS Code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.