'Compile errors in react-native-fs

I am building a small react app the has a main front page and can show static html pages. My plan is to store the static html pages as html files and load them passed on parameters in the URL.

I am trying to use react-native-fs to access the html files in my react app but it fails to compile of the during the import:

import fs from "react-native-fs";

The error I receive is:

./node_modules/react-native-fs/FS.common.js
SyntaxError: .../node_modules/react-native-fs/FS.common.js: Unexpected token, expected "," (30:29)

  28 | };
  29 | 
> 30 | var normalizeFilePath = (path: string) => (path.startsWith('file://') ? path.slice(7) : path);
     |                              ^
  31 | 
  32 | type MkdirOptions = {
  33 |   NSURLIsExcludedFromBackupKey?: boolean; // iOS only

I'm using react v16.12.0 and react-native-fs v2.16.2

I have tried changing the version of react-native-fs, reinstalling it, deleting node modules and reinstall again. I'm not sure what to do. I am open to using other libraries but I've already had trouble getting fetch to work.



Solution 1:[1]

I solved this problem with a fetch command instead of fs:

fetch("../pages/" + articleData.fileName).then(...)

Solution 2:[2]

Have same issue, when tried to use in web version (index.web.js) of react-native module react-native-fs.

import fs from "react-native-fs";

The error disappeared after moving this import to android (index.android.js) and ios(index.ios.js) versions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 double-beep
Solution 2 Alexandr Skachkov