Error when attempting to import 'fs' into .ts file

I’m building an app that uses currency exchange rates. I was attempting to set variables from values in a json file using the following:

import * as fs from 'fs';

export class CurrencyConverter {

  private rates: any;
  private usdtoeur: number;

  constructor() {
    this.rates = fs.readFileSync('./assets/oxr.json'); 
    this.usdtoeur = this.rates.EUR;
  }
}

for some reason I keep on getting the following error:
[ts] Cannot find module 'fs'.

I have updated all of my node modules to their compatible versions and this still has not fixed the problem.
Anyone know what is going wrong here?

What is fs supposed to do?? Support filesystem stuff? Then use File instead

Isnt fs a nodejs server side thing?

If it isnt, then it is good to know your package.json and see how you added this as module

Use the File plugin from Ionic Native.
Using the filesystem module won’t work in an app.

ok thank you both! I’ll read up on File and see if I can implement it

@MattE after importing File and adding it to my constructor like so:

import { File } from '@ionic-native/file';

constructor(public navCtrl: NavController, private file: File) {
    
  }

I get the following error:

ERROR Error: Uncaught (in promise): Error: StaticInjectorError[File]: 
  StaticInjectorError[File]: 
    NullInjectorError: No provider for File!
Error: StaticInjectorError[File]: 
  StaticInjectorError[File]: 
    NullInjectorError: No provider for File!
    at _NullInjector.get (core.js:993)
    at resolveToken (core.js:1281)
    at tryResolveToken (core.js:1223)
    at StaticInjector.get (core.js:1094)
    at resolveToken (core.js:1281)
    at tryResolveToken (core.js:1223)
    at StaticInjector.get (core.js:1094)
    at resolveNgModuleDep (core.js:10878)
    at NgModuleRef_.get (core.js:12110)
    at resolveDep (core.js:12608)
    at _NullInjector.get (core.js:993)
    at resolveToken (core.js:1281)
    at tryResolveToken (core.js:1223)
    at StaticInjector.get (core.js:1094)
    at resolveToken (core.js:1281)
    at tryResolveToken (core.js:1223)
    at StaticInjector.get (core.js:1094)
    at resolveNgModuleDep (core.js:10878)
    at NgModuleRef_.get (core.js:12110)
    at resolveDep (core.js:12608)
    at c (polyfills.js:3)
    at c (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4744)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at <anonymous>

You need to import and add File to your list of providers in app.module.ts.

1 Like

@SigmundFroyd awesome thanks that worked

where would I find the reference to the json file I’m trying to import data from?
The file is oxr.json and is located at /assets/oxr.json.

I’m using the following command to test if it can find a file but it is outputting nothing.

console.log("dir: "+this.file.readAsText(this.file.dataDirectory + "/assets/", "oxr.json"));

Better use http for json ans other stuff in assets folder

http.get(’/assets/etc’)

Hi @MattE I need to know how to or what to use for encryption & decryption of files using ionic cordova file plugin ?

thanks in advance