Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 how to use fs module?

I'm using Angular 7.

I tried to use fs module on Typescript for open a directory. I always have this error: "Module not found: Error: Can't resolve fs" types@node and file-system are installed.

My code:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit  {
  title = 'my-app';

  constructor() {

  }

  ngOnInit() {
    console.log(fs);
  }
}

Can you help me ?

Node : v10.14.0 Npm: v6.4.1 Angular CLI: v7.1.1

like image 273
Eng Avatar asked Dec 06 '18 17:12

Eng


2 Answers

'fs' is a library for NodeJS runtime, but the end product of the Angular pipeline bundler is a frontend SPA client which runs in a browser. If you need to load a file into the client from the user's local filesystem then you need to use browser native variant e.g. take a look at handling <input type="file"> elements.

like image 190
Alex Rempel Avatar answered Oct 19 '22 12:10

Alex Rempel


fs module is not available in Angular. Use the below package to use fs module.

npm install file-system --save

Example:

var fs = require('file-system');
 
fs.mkdir('1/2/3/4/5', [mode], function(err) {});
fs.mkdirSync('1/2/3/4/5', [mode]);
fs.writeFile('path/test.txt', 'aaa', function(err) {})

Reference: https://www.npmjs.com/package/file-system

like image 2
Sathia Avatar answered Oct 19 '22 11:10

Sathia