1

I am using rn-nodeify for enabling the use of pdf2json in React Native. pdf2json uses fs for loading files using the method readFileSync. I am getting this error when I try to use the library:

fs.readFileSync is not a function. (In 'fs.readFileSync(_basePath + fieldName, 'utf8')', 'fs.readFileSync' is undefined)

I cannot find any support for this issue. Any pointers appreciated.

Edit: Please note that I am not trying to run this in a browser. This pertains to react-native i.e. it runs on a device, and the code should have access to the file system.

Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • Possible duplicate of [readFileSync is not a function](https://stackoverflow.com/questions/37418513/readfilesync-is-not-a-function) – eli-bd Apr 14 '18 at 21:12
  • Checkout this [comment](https://github.com/tradle/rn-nodeify/issues/11#issuecomment-270061819), you can try if async is working or not. – Pritish Vaidya Apr 14 '18 at 21:14
  • Thanks for the response Pritish! That looks relevant. Since the method call is in a 3rd party library, I would prefer to try other options before changing that. Do you know how I can make fs point to react-native-fs instead of react-native-level-fs? – Bonton255 Apr 14 '18 at 21:21
  • I don't know if the react-native supports the synchronous functions. You may use async await in your async func as `let file = await fs.readFile` and see if that works. – Pritish Vaidya Apr 14 '18 at 21:38

1 Answers1

1

FWIW, I came across this problem while writing tests in ES6/latest JS - fixed this by changing the import from:

import { fs } from 'fs';

to

import fs from 'fs';

Notice the unnecessary {} - fs is the default export from the fs node module and should be imported in the latter way

For a better explanation on default & named exports please see this SO discussion

sami
  • 723
  • 2
  • 9
  • 23