react-native-dotenv: error: Error: Unable to resolve module @env

  • [] Asked question in discussions (would if I knew where this was?)
  • Tried the troubleshooting Wiki
  • [] Followed the migration Wiki (fresh install did not apply)

Describe the bug A clear and concise description of what the bug is.

Fresh install getting

error: Error: Unable to resolve module @env from /Users/[USER]/github/[APP]/screens/Map.js: @env could not be found within the project.

babel.config.js based on documentation as folows:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    //plugins: ['react-native-reanimated/plugin'],
    plugins: [
      'module:react-native-dotenv',
      'react-native-reanimated/plugin',
      // ["module:react-native-dotenv", {
      //   "envName": "APP_ENV",
      //   "moduleName": "@env",
      //   "path": ".env",
      //   "blocklist": null,
      //   "allowlist": null,
      //   "blacklist": null, // DEPRECATED
      //   "whitelist": null, // DEPRECATED
      //   "safe": false,
      //   "allowUndefined": true,
      //   "verbose": false
      // }]
    ]
  };
};

To Reproduce Steps to reproduce the behavior:

  1. npm install react-native-dotenv
  2. add .env to root dir
  3. config babel.config.js as shown above
  4. add ```import {ENV_KEY} from ‘@env’;
  5. See error

Expected behavior A clear and concise description of what you expected to happen.

I expect @env to be defined so I can import from the .env

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: iOS simpulator 15.2, iPhone 11
  • Browser: RN app there is not browser at play

Additional context Add any other context about the problem here.

Have tried killing metro and simulator, but problem presisits

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 29 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I get that devs got to eat too, but since this library does not work and only addresses bugs when paid, I found this one

Simple and works, no need to modify babble, just standard modern auto-link and pods

react-native-dotenv

it works for me! thanks a lot

Hey, thank you for opening this issue! 🙂 To boost priority on this issue and support open source please tip the team at https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/308

well when I kept API.cache(false) it worked. I have no idea why.

when i use babel-plugin-module-resolver,it does’t work. module.exports = { presets: [‘module:metro-react-native-babel-preset’], plugins: [ [ require.resolve(‘babel-plugin-module-resolver’), { alias: { ‘@utils’: ‘./libs/utils’, ‘@sdk’: ‘./libs/sdk’, ‘@env’: ‘./node_modules/react-native-dotenv’, }, }, ], [ ‘module:react-native-dotenv’, { moduleName: ‘@env’, path: ‘.env’, blacklist: null, whitelist: null, safe: false, allowUndefined: true, }, ], ], }; Unable to resolve module fs , ‘const {readFileSync} = require(‘fs’)’ I have installed this libary as devDependences!

Typically you can avoid this by using “react-native-dotenv” as your “moduleName”.