react-native-dotenv icon indicating copy to clipboard operation
react-native-dotenv copied to clipboard

Module `path` does not exist in the Haste module map

Open realtebo opened this issue 7 years ago • 51 comments

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

realtebo avatar Apr 16 '18 20:04 realtebo

Same here. Any updates from your side?

Rubenvdveen avatar Apr 19 '18 11:04 Rubenvdveen

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';

valerybugakov avatar Apr 20 '18 07:04 valerybugakov

I'm using the first syntax

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

realtebo avatar Apr 21 '18 11:04 realtebo

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

adamivancza avatar Apr 26 '18 10:04 adamivancza

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 avatar Apr 30 '18 03:04 patsissons

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 avatar May 01 '18 03:05 patsissons

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

mxmzb avatar May 04 '18 12:05 mxmzb

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

mohamadswaidan avatar May 09 '18 19:05 mohamadswaidan

Thank you @patsissons !

Martian2Lee avatar Jul 10 '18 11:07 Martian2Lee

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!

sam-warren avatar Jul 24 '18 22:07 sam-warren

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

gluix20 avatar Jul 30 '18 18:07 gluix20

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.

ewal avatar Aug 14 '18 05:08 ewal

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?

NicWickman avatar Aug 18 '18 19:08 NicWickman

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

mahgolfa avatar Aug 27 '18 05:08 mahgolfa

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

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

eyeamkd avatar Sep 14 '18 15:09 eyeamkd

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

piuccio avatar Sep 25 '18 04:09 piuccio

on RN 0.57.1 same issue

waveChand avatar Sep 25 '18 07:09 waveChand

its fixed by this https://github.com/jquense/yup/issues/216#issuecomment-389789014

sovanndyul avatar Oct 02 '18 09:10 sovanndyul

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

aknow2 avatar Oct 03 '18 07:10 aknow2

@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

cortehz avatar Oct 07 '18 12:10 cortehz

use ./path instead of path solved my problem

xlyasdasd avatar Oct 11 '18 11:10 xlyasdasd

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 👊

maxbeech avatar Nov 16 '18 22:11 maxbeech

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

adamyoui avatar Dec 14 '18 00:12 adamyoui

@maxbeech's tip worked for me!

PrimeTimeTran avatar Dec 15 '18 11:12 PrimeTimeTran

@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';

magna25 avatar Dec 22 '18 06:12 magna25

same error here. cannot resolve .

geminiyellow avatar Dec 27 '18 05:12 geminiyellow

same with me on RN version 0.57+

jamesmcn1 avatar Jan 21 '19 20:01 jamesmcn1

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

If you use yarn, write yarn instead of npm

DylanJu avatar Jan 24 '19 08:01 DylanJu

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

eugbash avatar Jan 24 '19 09:01 eugbash

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

vincentbriglia avatar Jan 24 '19 09:01 vincentbriglia