4

I am trying to read a list of files from a local directory using Angular 5-.

However i get the following error:

TypeError: fs.readdir is not a function

My .ts file is below:

import { Component, OnInit } from '@angular/core';
import * as fs from 'fs';

@Component({
  selector: 'app-review',
  templateUrl: './review.component.html',
  styleUrls: ['./review.component.css']
})
export class ReviewComponent implements OnInit {

  constructor() { }


  ngOnInit() {

    var fs = require('fs');

    fs.readdir('./assets', function (err, files) {
      // "files" is an Array with files names
            console.log(files);

  });
  }

}

Note have done npm install fs --save.

Also please help me finding datemodified info of the files.

JEY
  • 6,973
  • 1
  • 36
  • 51
Asha
  • 57
  • 1
  • 1
  • 3
  • 2
    Are you trying to do this in the browser? If so, that's not possible - you can only read the file system in node, on the server. – Thor Jacobsen Jun 14 '18 at 06:42
  • Am trying to do this on browser....Any thoughts how could I achieve that? – Asha Jun 15 '18 at 07:11
  • 2
    You can't - it's not possible, as I said. The browser cannot read the filesystem, except for uploading files - and you can't read the file contents in that case. That would be a huge security risk if possible. – Thor Jacobsen Jun 15 '18 at 09:01
  • https://stackoverflow.com/questions/44871259/angular4-read-local-files/44871842#44871842 – Mohit Kumar Gupta Feb 10 '19 at 22:20

0 Answers0