zetachang / react-native-dotenv

A Babel preset let you import application configs from .env file (zero runtime dependency)
MIT License
915 stars 68 forks source link

Unable to resolve module "path" #30

Closed pdaniloais closed 7 years ago

pdaniloais commented 7 years ago

I added react-native-dotenv with yarn add react-native-dotenv --save-dev

I created a 2e2test.env in the root directory with:

ENV=e2etest

I call react-native-dotenv from my code:

import { ENV } from 'react-native-dotenv';

but when I run my app in the iOS simulator, I have the following error:

Unable to resolve module `path` from `/node_modules/react-native-dotenv/index.js`: Module does not exist in the module map

Ideas ?

mengyiyuan commented 7 years ago

Hello, I have exactly the same issue. May I know how you solved it? Thanks a lot!

clementdevosmc commented 7 years ago

Hey i've got the same issue, can you give us more info on how you fixed it?

clems36 commented 7 years ago

I had the same issue until i added react-native-dotenv to my .babelrc presets. Hope this can help.

vomchik commented 6 years ago

I have the same issue with react-native 0.50.4. Any ideas?

dlucidone commented 6 years ago

@clementino36 How to add it to presets?

clems36 commented 6 years ago

@dlucidone here's my .babelrc

{
    "presets": [
        "babel-preset-expo",
        "react-native-stage-0",
        "react-native-dotenv"
    ]
}
lucidtheory commented 6 years ago

For me it was an issue with having a space in my absolute file path

If you do need to do this you may need to run rm -rf ios/build/ModuleCache/*

so that your builds dont run off of the old file path

peterholcomb commented 6 years ago

If anyone runs into this on RN 0.57 or greater, you may need to adjust the .babelrc to be

{
  "presets": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"]
}
jmasaki24 commented 5 years ago

My .babelrc is { "presets": [ "module:metro-react-native-babel-preset", "modle:react-native-dotenv" ] } But I still get this issue. Would there be an alternate solution? I'm using node 8.9.1 and npm 4.6.1

jamesmcn1 commented 5 years ago

@jmasaki24 hopefully wasn't because of the typo there :)

jamesmcn1 commented 5 years ago

changed babel.rc to babel.config.js worked for me:

this is with RN 57 and babel 7. you'll also need to ensure you've got all the correct babel 7 dependencies installed via npm, and ensure you've got the lastest version of react-native-dotenv

module.exports = function (api) {
  api.cache(true)
  return {
    "presets": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"]
  };
}
jmasaki24 commented 5 years ago

@jamesmcn1 I don't even know man, I just do everything from scratch again and it works....

raymanhjn commented 5 years ago

Tried to solve as @jamesmcn1 said, and also reset the simulator and it works

fabOnReact commented 5 years ago

@raymanhjn and @jamesmcn1 I am also experiencing this issue on ios I have babel.config.js correctly configured and the app works fine on my Ubuntu 18 OS running android, but does not work on Mac OS High Sierra and the Iphone 10 simulator (even after resetting the simulator and deleting node_modules)

I am using react-native-dotenv v 2.0

the error message says Cannot find module react-native-dotenv from 'Users/fabriziobertoglio/Documents/sourcecode/native/surfnative' (null)

react native 0.57.8

reading the metro logs took me to this other error jeste-haste-map Cannot find module './blacklist'

Thanks for the help

fredsvanelli commented 5 years ago

I've just performed a clean install of React Native 0.60.5, Babel 7.6.0 and react-native-dotenv 0.2.0,

my babel.config.js file:

module.exports = {
  presets: ['module:metro-react-native-babel-preset','module:react-native-dotenv']
};

And I got the same error:

Unable to resolve the module 'path' from /node_modules/react-native-dotenv/index.js: Module 'path' does not exist in the Haste module map

Anyone found the solution yet?

vinurs commented 5 years ago

i run into this in react-native 0.61.2

rluncasu commented 5 years ago

This worked for me:

npm i --save-dev react-native-dotenv
npm i --save-dev metro-react-native-babel-preset

create .babelrc

{
  "presets": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"]
}

I was still seeing the error at this point then ran:

expo start -c
drahosistvan commented 5 years ago

changed babel.rc to babel.config.js worked for me:

this is with RN 57 and babel 7. you'll also need to ensure you've got all the correct babel 7 dependencies installed via npm, and ensure you've got the lastest version of react-native-dotenv

module.exports = function (api) {
  api.cache(true)
  return {
    "presets": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"]
  };
}

this babel.config.js file resolved my issue along with yarn start --reset-cache

helderberto commented 5 years ago

This worked to me:

Installed these packages:

$ yarn add -D metro-react-native-babel-preset
$ yarn add -D react-native-dotenv 

.babelrc

{
  "presets": [
    "module:metro-react-native-babel-preset",
    "module:react-native-dotenv"
  ]
}

Created a .env file in the root of the project:

API_HOST=<http-url>

After that file .env could be loaded in the code:

import { API_HOST } from 'react-native-dotenv';

console.log(API_HOST);
cwilby commented 4 years ago

After performing these changes, make sure you clear cache by running npm start -- --reset-cache.

citadelio commented 4 years ago

Adding --reset-cache worked for me I'm using npx so its npx react-native start --reset-cache

chassierPA commented 4 years ago

I added in my babel.config.js

const plugins = [
 // ... some plugins ...
]

module.exports = {
  presets: ['module:metro-react-native-babel-preset', 'module:react-native-dotenv'],
  plugins: [...plugins],
};

Works fine too for react-native > 0.60