Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native-dotenv path could not be found within the project or in these directories:

Ive installed react-native-dotenv in my react-native-project (non expo) and Im getting this error

error: Error: Unable to resolve module path from C:\XXX\XXX\XXX\node_modules\react-native-dotenv\index.js: path could not be found within the project or in these directories:
  node_modules
  ..\..\..\..\..\node_modules
  1 | const {readFileSync, statSync} = require('fs')
> 2 | const path = require('path')
    |                       ^
  3 | const dotenv = require('dotenv')
  4 |
  5 | function parseDotenvFile(path, verbose = false) {

Read up online and people say you need to modify your babel.config in which I did

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module:react-native-dotenv',
      {
        envName: 'APP_ENV',
        moduleName: '@env',
        path: '.env',
      },
    ],
  ],
};

But it still reproduces the same error and most of these fixes are for expo, Ive never worked with expo and I tried the following

https://stackoverflow.com/a/64227089/5413196

https://github.com/goatandsheep/react-native-dotenv/wiki/Migration-Guide

https://github.com/facebook/react-native/issues/29314

And I am still getting the same error, could someone please advise how to fix ?

like image 441
Faziki Avatar asked Oct 18 '25 06:10

Faziki


1 Answers

react-native-dotenv author here: as said in the comment above, Babel config looks great. Make sure to install the lib as a dev dependency.

like image 58
Kemal Ahmed Avatar answered Oct 19 '25 21:10

Kemal Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!