0

When I try to use

import { readFileSync } from 'fs';

I get the error

Module not found: Error: Can't resolve 'fs'

In fact, when I use readFileSync in my program in VS even the intellisense works fine, but when I run ng serve it comes up with a compiler error.

I've spent hours looking at other SO posts and searching the internet. Nothing has worked. For example, have tried setting "types": ["node"] where previously it was empty. Have tried using a strange /// syntax for importing, but that didn't work either.

I've also tried strange syntax like import { fs } from '@types/node';.

I'm using fs for the following socket.io call:

this.socket = io('https://12.123.1245:1234', { ca: readFileSync('./assets/cert.pem'), rejectUnauthorized: false });
Milo
  • 3,365
  • 9
  • 30
  • 44
Philip
  • 638
  • 1
  • 8
  • 22
  • 1
    and what is `fs`? – Liam Aug 12 '19 at 13:55
  • Are you trying to [do this](https://stackoverflow.com/questions/53656426/angular-7-how-to-use-fs-module)? In which case you need to read [this comment](https://stackoverflow.com/questions/53656426/angular-7-how-to-use-fs-module#comment94171732_53656426) – Liam Aug 12 '19 at 13:57
  • Yeah thanks @SagarChaudhary, obviously it's an npm package, which one... – Liam Aug 12 '19 at 13:57
  • I thought you didnt knew. Not the case. – Sagar Chaudhary Aug 12 '19 at 13:58

1 Answers1

1

fs is a nodeJs only module. When you run "ng serve" your app will be compiled and run in your browser, so you can not use this mode. To read from a file without user action ( select file ) is not possible for security reason on your browser

Thomas
  • 1,058
  • 8
  • 15