Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Module path does not exist in the Haste module map #39

Open
realtebo opened this issue Apr 16, 2018 · 51 comments
Open

Module path does not exist in the Haste module map #39

realtebo opened this issue Apr 16, 2018 · 51 comments

Comments

@realtebo
Copy link

realtebo commented Apr 16, 2018

error: bundling failed: Error: Unable to resolve module `path` from 
`/Users/realtebo/Documents/myApp/node_modules/react-native-dotenv/index.js`: 
Module `path` does not exist in the Haste module map

I got the above error after adding react-native-dotenv to my project when I run react-native run-ios

Some info about my software context:

macOS 10.13.4
nodeJs v8.11.1
npm 5.6.0
react-native-cli: 2.0.1
react-native: 0.55.2

This my package.json sections about deps

  "dependencies": {
    "lodash": "^4.17.5",
    "native-base": "^2.4.2",
    "react": "16.3.1",
    "react-native": "^0.55.2",
    "react-native-device-info": "^0.21.5",
    "react-native-navigation": "^1.1.443",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "devDependencies": {
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "eslint": "^4.19.1",
    "jest": "22.4.3",
    "react-native-dotenv": "^0.1.1",
    "react-test-renderer": "16.3.1"
  },

I already tried a brutal cleaning of my project

watchman watch-del-all  
rm -rf ~/.npm 
rm -rf ios/build 
rm -rf /tmp/metro-bundler-cache-* 
rm -rf /tmp/haste-map-react-native-packager-* 
rm -rf $TMPDIR/react-* 
rm -rf node_modules 
npm install

...but without success

@Rubenvdveen
Copy link

Same here. Any updates from your side?

@valerybugakov
Copy link

valerybugakov commented Apr 20, 2018

How do you import environment variables in your apps? I had the same issue and resolved it by adding react-native-dotenv plugin to .babelrc and importing vars via:

import { VAR1, VAR2 } from 'react-native-dotenv';

instead of

import * from 'react-native-dotenv';

@realtebo
Copy link
Author

I'm using the first syntax

import { VAR1, VAR2 } from 'react-native-dotenv';

@adamivancza
Copy link

I had similar problem - what helped was running npm start -- --reset-cache command.

@patsissons
Copy link

I'm running into this too, specifically from an expo setup. Not sure what exactly is happening, but it looks like the plugin isn't getting run so it doesn't bother removing the imports and then they are processed by the bundler which complains because react-native-dotenv pulls in path which isn't availble for react-native. No clue why the plugin isn't getting run though.

@patsissons
Copy link

patsissons commented May 1, 2018

My issue was related to using a transformer in the app.json (typescript). This transformer was altering the source before the plugin had a chance to swap in the env values. Easy work around, just create an env.js that imports all your env vars and then re-exports them (you'll also want an env.d.ts to define the shape).

// env.js
import { TEST } from 'react-native-dotenv';

export const env = {
  TEST: TEST,
};
// env.d.ts
export interface Env {
  TEST: string;
}

export const env: Partial<Env>;

@mxmzb
Copy link

mxmzb commented May 4, 2018

@patsissons for me your solution from here istanbuljs/babel-plugin-istanbul#116 (comment) did it (thanks! 👍). i hope though, this is just a temporary fix.

@mohamadswaidan
Copy link

make sure you have .env file created and variables are initiated.

@Martian2Lee
Copy link

Thank you @patsissons !

@sam-warren
Copy link

sam-warren commented Jul 24, 2018

My issue was that I was operating in a different branch and had forgotten to add "react-native-dotenv" to my .babelrc presets. Your .babelrc presets should look like this:
"presets": ["babel-preset-expo", "react-native-dotenv"],

Hope this helps someone!

@gluix20
Copy link

gluix20 commented Jul 30, 2018

From RN 0.56+ the .babelrc presets should have the "module" key:
"presets": [ "react-native", "module:react-native-dotenv" ]

@ewal
Copy link

ewal commented Aug 14, 2018

Any updates on this? I'm trying to upgrade to RN 0.56 but get the same error message when building. I've also added the module prefix in .babelrc but with no success.

@NicWickman
Copy link

I'm getting this error with firebase-functions...Pat, can you fill in the blanks on your instructions for somebody who really doesn't understand what's happening?

@mahgolfa
Copy link

sometimes when u import something, it automatically imports some other excessive modules like import Path from ... , take a look if it has happened to ur project

@eyeamkd
Copy link

eyeamkd commented Sep 14, 2018

I had similar problem - what helped was running npm start -- --reset-cache command.

got the same problem resolved it with your help,thank you

@piuccio
Copy link

piuccio commented Sep 25, 2018

Seeing this issue when upgrading to RN 0.57. I've tried all suggestions and nothing worked for me. In the end I switched to react-native-config, it's slightly more annoying to setup but it works with the same .env files

@waveChand
Copy link

on RN 0.57.1 same issue

@sovanndyul
Copy link

its fixed by this jquense/yup#216 (comment)

@aknow2
Copy link

aknow2 commented Oct 3, 2018

@sovanndyul thank you! npm add @babel/runtime work for me.

@cortehz
Copy link

cortehz commented Oct 7, 2018

@sovanndyul I've tried everything including npm add @babel/runtime but still doesn't work. My path that says doesn't exist is from a component that I created inside a components folder. Like so: error: bundling failed: Error: Unable to resolve module pathfrom/Users/iCortehz/Documents/React-Native/albums/src/components/header.js: Module path does not exist in the Haste module map

@xlyasdasd
Copy link

use ./path instead of path solved my problem

@maxbeech
Copy link

Removing node_modules and re-adding didn't work for me.

BUT as crazy as it sounds, restarting the computer worked! Who knows what it was that actually needed stopping and re-starting, but if you're stuck with this issue, worth a go 👊

@adamyoui
Copy link

npm install --save path fixed it for me on Android.

@PrimeTimeTran
Copy link

@maxbeech's tip worked for me!

@magna25
Copy link

magna25 commented Dec 22, 2018

@xlyasdasd's solution worked for me. If you are trying to import another component from same directory use import Component './filename'; instead of import Component 'filename';

@geminiyellow
Copy link

same error here. cannot resolve .

@eugbash
Copy link

eugbash commented Jan 24, 2019

It seems that what is needed to be cleaned depends on the react-native bundler version.

So if cache is the case you can try:

  • on macOS:
    rm -rf $TMPDIR/haste-map-*
    rm -rf $TMPDIR/metro-cache-*

  • on Windows:
    remove haste-map-* cache files in the C:\Users{User}\AppData\Local\Temp folder

For production build --reset-cache flag can be used, i.e:
react-native bundle --reset-cache

@vincentbriglia
Copy link

when you're using typescript, and you're using react-native-typescript-transformer you were asked to add

  transformer: {
    babelTransformerPath: require.resolve('react-native-typescript-transformer'),
  },

and if you want to use this component with the latest RN (0.57.8) you should not have a .babelrc but a babel.config.js instead that container

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

and then use @patsissons solution

that worked for me

@jamesmcn1
Copy link

jamesmcn1 commented Jan 24, 2019

yes - I fixed in the end by writing a babel.config.js file like this:

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

and running yarn start -- --reset-cache locally (as I installed thru yarn). Make sure you've got the latest babel core, and presets downloaded aswell eg @babel/runtime. (RN 0.57.8, babel 7)

@JaesungNa
Copy link

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

it worked for me! thanks :)

@bugsniper
Copy link

@xlyasdasd 's solution worked for me.

@MyDinnerIsReady
Copy link

@maxbeech Unbelievable! Restart the computer is really work!

@Sbphillips19
Copy link

In case some people run into this issue check the file specified. Somehow this randomly gets added sometimes:

import { isAbsolute } from 'path';

Just delete and reset cache and re-run

@te-online
Copy link

te-online commented Apr 4, 2019

My issue was related to using a transformer in the app.json (typescript). This transformer was altering the source before the plugin had a chance to swap in the env values. Easy work around, just create an env.js that imports all your env vars and then re-exports them (you'll also want an env.d.ts to define the shape).

// env.js
import { TEST } from 'react-native-dotenv';

export const env = {
  TEST: TEST,
};
// env.d.ts
export interface Env {
  TEST: string;
}

export const env: Partial<Env>;

@patsissons Thanks for this great workaround, when using typescript and react-native-typescript-transformer. However, you missed to explain how this changes the usage of the env variables because of the interface you created:

// App.tsx
...
import { env } from './env';
...

console.log("TEST is", env.TEST);

@patsissons
Copy link

I think it may be possible to use export const TEST = TEST but I didn't test it.

@te-online
Copy link

te-online commented Apr 5, 2019

I think, what you can do it this:

// env.js
import { TEST } from 'react-native-dotenv';
export {
  TEST
}
// env.d.ts
export const TEST: string;
// App.tsx
...
import { TEST } from './env';
...

console.log("TEST is", TEST);

@ghost
Copy link

ghost commented May 13, 2019

I had similar problem - what helped was running npm start -- --reset-cache command.

If you use yarn, write yarn instead of npm

Getting this: error Command failed with exit code 1.

@ghost
Copy link

ghost commented May 13, 2019

I got this, I was adding some style, then I saved changes and stopped working. Weird for me the fact is trying to resolve ´path´from App.js. I have tried all the solutions of the screenshot. I restarted my iMac and getting the same.
You can see all the info on: https://es.stackoverflow.com/questions/263183/failed-to-load-bundle-unable-to-resolve-module-path-module-pathdoes-not-ex

IMG_9748

@SeanFelipe
Copy link

yarn cache clean fixed the problem for me.

@jako-hub-old
Copy link

If someone still has the same error, make sure module:react-native-dotenv is defined in the babel presets.
babel.config.js:

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

@LannyCodes
Copy link

use './src/navigate/StackNavigate' instead of 'src/navigate/StackNavigate' works for me !

@CLantigua2
Copy link

I did a lot of the steps previously stated but what ultimately got it to work for me was what was suggested by @maxbeech in restarting my machine. It was the last thing I tried because it didn't sound like it would work but it did haha.

@jsina
Copy link

jsina commented Sep 16, 2019

as @vincentbriglia said, when you're using typescript and can handle it on babel.config.js
here it is:

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

@CyrusZei
Copy link

CyrusZei commented Dec 6, 2019

I had similar problem - what helped was running npm start -- --reset-cache command.

If you use yarn, write yarn instead of npm

that fixed it for me

@elektronik2k5
Copy link

I faced the same issue when trying to export { ENDPOINT_URL } from 'react-native-dotenv' in a TS project. The ugly workaround is:

import { ENDPOINT_URL as DOTENV_ENDPOINT_URL } from 'react-native-dotenv'

export const ENDPOINT_URL = DOTENV_ENDPOINT_URL

🤦‍♂️

@Alertis
Copy link

Alertis commented Jan 5, 2020

I faced the same issue when trying to export { ENDPOINT_URL } from 'react-native-dotenv' in a TS project. The ugly workaround is:

import { ENDPOINT_URL as DOTENV_ENDPOINT_URL } from 'react-native-dotenv'

export const ENDPOINT_URL = DOTENV_ENDPOINT_URL

man_facepalming

that fixed it for me 😒

@mattiasahlsen
Copy link

"yarn start -c" worked for me with expo, it's for clearing cache

@switch13
Copy link

reinstalling the offending module, then switching between npx worked for me in RN60.0.

  1. npm install --save react-native-copilot (my offending module)
  2. npx react-native run-ios
  3. react-native run-ios

@dgana
Copy link

dgana commented Mar 15, 2020

@maxbeech's tip worked for me also! I don't know how but it does the job

This was referenced May 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests