React Native: Unable to resolve module fs

Install react-native-fs (follow the instructions at de link), so weather the error persist, enter at the directory ('./node_modules/tfjs-image-recognition-base/build/commonjs/env/) search by the file: (creatFileSystem.js) and edit:

fs = require('fs')

to:

fs = require('react-native-fs')

I don't know if this is recommended way, but was the only that worked for me.


I ended up using 'rn-nodeify' to include fs into React Native. You can use most of the node core modules this method. Install it with npm:

npm install rn-nodeify

Then in package.json file, add the following line in "scripts" to specify which modules you want to include in your RN project. For example, I used fs, crypto and https, and the line goes

"postinstall": "node_modules/.bin/rn-nodeify --install crypto,fs,https --hack"

React Native applications do not run in the Node.js environment (it is only used by the packager to actually serve and compile your application bundle).

Since your app is running inside JS VM on either iPhone or Android, the only way to access filesystem is to use react-native-fs or any other 3rd party module that utilises bridge connection to talk to the platform specific native APIs.


it took me a while to find the issue, I'm sharing it as it might be useful for future reference (using react native with Expo):

One of my coworkers accidentally imported in one of our react components app.config.js in the project root to read some configuration rather than using expo-constants.

it was causing it to read the .env file from the react native wrapper that obviously doesn't have the fs lib.. this is the first line of our app.config.js:

import 'dotenv/config';

the correct way for reading settings inside app.config is:

import Constants from 'expo-constants';

const appConfig = Constants.manifest;