Unable to resolve module fs - react-native-dotenv

1.4k Views Asked by At

I am doing an online course about React-Native and Firebase but I came across an error.

Here it is : Error: Unable to resolve module fs from Documents/code/ChatApp/node_modules/react-native-dotenv/index.js:

enter image description here

I've seen responses that fs is not available on react-native, is that true ?

Why install a package so that in the end it can't be used?

thank you in advance for your answers

3

There are 3 best solutions below

0
Abe On

No, fs is not available in React Native. You can use the react-native-fs package instead.

https://github.com/itinance/react-native-fs

0
vinayr On

That online course might be referring to old version of react-native-dotenv. To make it work with the new version, make these minor changes to your code as explained in this wiki. No need to install fs.

  1. If you have a code like this
import { API_URL, API_TOKEN } from 'react-native-dotenv';

change it to

import { API_URL, API_TOKEN } from '@env';
  1. Make sure module:react-native-dotenv is present in babel.config.js file. Example -
module.exports = {
  presets: ["module:metro-react-native-babel-preset"],
  plugins: [
    "module:react-native-dotenv",
    ...
  ]
};
0
Kemal Ahmed On

react-native-dotenv author here: reinstall the library as a dev dependency

npm i -D react-native-dotenv