[Fixed] Import Data in Angular

Issue

I tried using csv files in my Angular projects like this:

import * as data1 from "./data.csv";
import * as data2 from "./data2.csv";

They are located in the same folder as the .ts i am trying to use them with.
I got following error:

Cannot find module ‘data.csv’

What I also tried was moving them to the assets folder:

import * as data1 from '/assets/data.csv';

But the error still shows up.
What am I doing wrong?

Solution

Read the csv file content using HttpClient, for example:

constructor (private http: HttpClient) {}

readCsvData () {
   this.http.get('path.to.csv')
   .subscribe(
        data => console.log(data),
        err => console.log(err)
    );
}

Leave a Reply

(*) Required, Your email will not be published