63

I'm getting an error in React Native saying it can't resolve a module. It's saying a certain folder doesn't exist but the path isn't accurate. It is telling me that Directory /Users/user/Desktop/RNApp/app/app/containers doesn't exists. I have no idea where that second app is coming from in the path though. My directory looks like this

enter image description here

This is how I am exporting my container components in app/containers/index.js

export RNAppNavigator from './Navigator/RNAppNavigator'
export SplashContainer from './Splash/SplashContainer'
export AppContainer from './App/AppContainer'

So I should be able to import them like import {AppContainer} from '~/containers correct? I don't get an issue with any other components. Just the AppContainer.

Thanks!

maxwellgover
  • 6,661
  • 9
  • 37
  • 63
  • 1
    Once I made a file called `Index.js` but imported `index.js`. Hello Case-sensitive-imports, my old friend. – KeshavDulal Jan 08 '21 at 13:31
  • Hi , this was 2016 but i have same error, if you know answer pls help. – Tuzi Jan 17 '22 at 23:58
  • Hi, this is 2023 and I have same error, is there anyway to fix this WITHOUT restarting the Metro? When I restart Metro everything is fine, but this take too much time, everytime I create new screen I have to restart it. – Huan Huynh Feb 25 '23 at 07:06

29 Answers29

190

react-native start --reset-cache solved the issue. https://github.com/facebook/react-native/issues/1924

maxwellgover
  • 6,661
  • 9
  • 37
  • 63
24

If you are having similar issues with pure components or classes, ensure you are using .js extension instead of .jsx.

Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
  • I wouldn't even use my components because I was using `.jsx`, does that mean React Native does not support jsx files? – Gilbert R. Jun 22 '19 at 00:27
  • Yes, I believe React native doesn't have support to JSX files. – Pablo Darde Jun 22 '19 at 17:32
  • 1
    Just to clarify, I am using Expo, I think it's Expo that does not support `.jsx` files, I wouldn't know with the pure native approach though. – Gilbert R. Jun 22 '19 at 23:14
  • 1
    @GilbertR., I built an app with pure React Native, and it doesn't support `.jsx` files too. You can check it [here](https://github.com/darde/react-native-sidemenu) – Pablo Darde Jun 23 '19 at 00:36
  • i had TSX and jsx in expo , later I ejected , every thing worked with jsx and tsx extension apart from one custom import that was in jsx (was working in expo), stopped working in bare flow, thanks to your answer, I renamed the file and worked like a charm – Furquan Apr 24 '20 at 10:52
  • 1
    I can't understand why `.jsx` won't be valid file extension for a react-native filename. – Cels Sep 17 '20 at 15:05
15

I was losing the will to live over this error, RN telling me that an imported file ./Foo did not exist when it was right there!

The actual underlying error was not a typo but actually in another file that ./Foo imported.

Be careful. If you are writing JSX anywhere (eg. in ./Bar):

<Bar>...</Bar>

then you must have:

import * as React from 'react' (or similar)

present in that file (./Bar).

When the syntactic sugar (angled brackets) is transpiled it naively spits out something like:

React.createComponent(...)

And if React has not explicitly been imported this reference will be invalid, so the evaluation of this dependent file subsequently fails.

Unfortunately the result is that consequently ./Foo (as well as ./Bar) will be unavailable in your app, and thus RN says unhelpful things like module not found and Indeed, none of these files exist.

Hope that helps.

ps. you can also experience similar misery if you have circular dependencies between files! Such fun!

Darius
  • 5,180
  • 5
  • 47
  • 62
6

For me, using expo, I just had to restart expo. ctrl + c and yarn start or expo start and the run on ios simulator or your device, whichever you're testing with.

Overcomer
  • 434
  • 5
  • 11
5

I had the same problem, I fixed that by below steps:

1-> delete node_modules by: rm -rf node_modules

2-> delte Pods folder inside ios folder: cd ios rm -rf Pods

3-> clear metro bunder: rm -rf ${TMPDIR:-/tmp}/metro-*

4-> reset cache npx react-native start --reset-cache

Now install node_modules and pods by below commands

1-> npm install

2-> cd ios

3-> pod install

4-> npx react-native run-ios

This work for me, Thanks

Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
  • 1
    It works for me like a charm. In the above steps, I think rm -rf ${TMPDIR:-/tmp}/metro-* command removes the metro cache and it solves the issue. +1 – Gavara.Suneel Jan 04 '23 at 17:21
4

Based on your folder structure, try import like this in index.js:

import { AppContainer } from './App/AppContainer';

If you are using a named export, that is if you are doing:

export class AppContainer [...]

If you are using a default export, such as:

export default class AppContainer [...]

The object destructuring will fail. You have to do instead:

import AppContainer from './App/AppContainer';
martinarroyo
  • 9,389
  • 3
  • 38
  • 75
max23_
  • 6,531
  • 4
  • 22
  • 36
  • Ok. I'll give it a try when I get home. i vaguely remember running into something like that a while back. I'll give it a shot. Thanks! – maxwellgover Dec 06 '16 at 14:18
  • 1
    Unfortunately this did not work. It just gives me another error. This time though it is saying a directory that actually exists doesn't exist lol. It is saying `Directory /Users/user/Desktop/RNApp/app/App/AppContainer doesn't exist` when it clearly does. I'm absolutely stumped. – maxwellgover Dec 07 '16 at 03:41
  • @staxwell In which file you are actually importing the AppContainer? Since you are saying export is done in app/containers/index.js. – max23_ Dec 07 '16 at 03:46
  • exporting is done in `app/containers/index.js` importing is done in `app/index.js` – maxwellgover Dec 07 '16 at 03:48
  • @staxwell Oh, can you try this: `import { AppContainer } from './containers/index';` – max23_ Dec 07 '16 at 04:16
  • Didn't work :( I'm betting there is a typo somewhere but I've been over it about a hundred times and haven't spotted anything. – maxwellgover Dec 07 '16 at 04:21
  • @staxwell Hmm, would you mind post the source for AppContainer.js? – max23_ Dec 07 '16 at 04:25
  • The code is up here :) https://github.com/MaxwellGover/react-native-pomodoro/tree/master/app/containers/App – maxwellgover Dec 07 '16 at 04:27
  • @staxwell Thanks. Can you change how you export the AppContainer to `export { default as AppContainer } from './App/AppContainer';` in `app/containers/index.js`. Then, import it using `import { AppContainer } from './containers/index';` in `app/index.js`. – max23_ Dec 07 '16 at 07:38
2

Hardware -> Erase all content and settings did the job for me (other things didn't).

illya yurkevich
  • 226
  • 3
  • 6
2

Make sure the module is defined in the package.json use npm install and then try react-native link

ashwath hegde
  • 606
  • 8
  • 12
2

If your file path is correct then check any one file contains

import React from './node_modules/react';

replace it with

import React, { Component } from 'react';
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
2

I also faced the same issue, now it is resolved. If you are facing issues with pure components or classes, make sure that you are using .js extension instead of .jsx.

Aamir
  • 152
  • 6
1

I'm using react-native CLI and I just restart rn-cli, ctrl+c to stop the process then npx react-native start

1
  1. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-*
  2. react-native start --reset-cache
1

using this worked for me npm cache add <package name><@version>.

Example:

npm cache add react-native-paper@0.1.9
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ola korede
  • 11
  • 1
  • 1
0

It looks like your error is coming from the outer index.js file, not this one. Are you importing this one as './app/containers'? because it should just be './containers'.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Matt Aft
  • 8,742
  • 3
  • 24
  • 37
  • In my /app/index.js file I `import {AppContainer} from '~/containers`. When I try `./containers` I get another error saying another directory doesn't exist somewhere else. – maxwellgover Dec 05 '16 at 23:56
  • It says it can't resolve module `./containers` when I try that. – maxwellgover Dec 06 '16 at 00:02
  • Not sure then, in AppContainer you're doing export default and not just export right? Other than that, try to rebuild your app on xcode and see if the errors go away. – Matt Aft Dec 06 '16 at 00:06
  • Yeah and I've already tried rebuilding. Hm. I'll figure it out and I'll let you know what I find out. Thanks again!! – maxwellgover Dec 06 '16 at 00:08
  • np, can you post a ss of your app/index.js? – Matt Aft Dec 06 '16 at 00:11
  • I've updated the question with app/index.js screen shot. – maxwellgover Dec 06 '16 at 00:20
  • it sets the root as app? because I remember seeing an error that it's looking for app/app/containers – Matt Aft Dec 06 '16 at 00:31
  • `~/containers` would tells `babel-root-import` to start at the `/app` folder and then look in containers folder. The `rootPathSuffix` in `.babelrc` specifies which folder is the root. – maxwellgover Dec 06 '16 at 00:31
  • and if you use ./containers, what error did it give? – Matt Aft Dec 06 '16 at 00:33
  • It says it's unable to resolve module ./containers from Navigator/RNAppNavigator.js Directory /Users/User/Desktop/RNApp/app/containers/Navigator/containers doesn't exist. I'm just getting all these weird paths. – maxwellgover Dec 06 '16 at 00:38
  • yea...no clue why it's going into Navigator folder to look for containers. – Matt Aft Dec 06 '16 at 00:42
  • when you run your package manager try 'react-native start --root' – Matt Aft Dec 06 '16 at 00:47
  • Ok I'll give it a shot in a few hours. Will this command launch the simulator as well? I know that `react-native run-ios` does. I feel like I have run the start command before but it didn't launch the simulator. – maxwellgover Dec 07 '16 at 13:44
  • No, but it will start up the packager for you. I think there's an issue with using RN and that library you're using for filepaths. – Matt Aft Dec 07 '16 at 16:02
  • OK. I might just go back to the default way of importing. Not worth the headache. – maxwellgover Dec 07 '16 at 16:03
0

I was able to solve this issue by adding a file extension '.js'. I was using Intellij and created a js file, but it did not have the file extension. When I did a refractor rename and changed my component from 'List' to 'List.js' react native then knew how to resolve it.

Grez.Kev
  • 247
  • 1
  • 6
  • 17
0

This is my project directory structure

enter image description here

And i use the import like this

enter image description here

And it is working do not forget to add dot before slash for example './src/container/home/profile/index'

Faris Rayhan
  • 4,500
  • 1
  • 22
  • 19
0

reload the app and the cause of this error is the changes of files location made in react-native dependency which other libraries like native-base point to.

To solve it you need to view the mentioned file and change the file location to the correct location.

0

Deleting the node folder and restarting works for me(run npm install after restarting)

chris
  • 194
  • 2
  • 10
0

In my case I needed to import using an extended file path

i.e:

WRONG: import MyComponent from "componentFolder/myComponent";

RIGHT: import MyComponent from "../myAppFolder/componentFolder/myComponent";

Both of these showed no Typescript errors, but only the bottom one worked.

Stuart Aitken
  • 949
  • 1
  • 13
  • 30
0

If you recently had a name change in git that only includes changing the case of file. MacOS will ignore the change. So, you will have to go to the file in console and change the name. for example if name change is menu.js -> Menu.js use terminal to do a manual rename after the pull.

mv menu.js Menu.js

Haseeb A
  • 5,356
  • 1
  • 30
  • 34
0

I had this error and then I realized that my package.json file was mostly empty. Make sure you have all the dependencies you have first.

use yarn add DEPENDENCY_NAME to add dependencies.

Stephen
  • 171
  • 3
  • 3
0

In react native .jsx is different file than .js. That's why I was getting this error. I was trying to import Comp.jsx when I needed to name the file Comp.js.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
0

In my case I got error like below,

App.js (0:1)
Unable to resolve module 'module://../components/Card.js'
(Device)
  Evaluating module://../components/Card.js
  Evaluating module://App.js.js
  Loading module://App.js

I cleared the above error,

  1. by checking the app.js import lines. File location was wrong. I made error in below line,

    import Card from "../components/Card";

  2. Also i didn't write export default app; this line.

fixedDrill
  • 183
  • 1
  • 13
0

If none of these worked for you try deleting your ‘node_modules’ folder, then run:

npm install

Hopefully this fixed it.

Relie Essom
  • 959
  • 9
  • 15
0

Unable to solve module 'module-name' is realated to the node modules packages and sometime you would need to clean the node_modules folder and reset the application. For beginners this would do the trick.

npm install

or

yarn install

Delete your node modules and reinstall if shows further errors.

Remember yarn and npm although its basically the same, they have yarn.lock and package-json.lock files, So also depends on the project.

0

I had the error and in my case i simply installed the module at the wrong location (i wasn't using the Terminal at the root of my project).

Check your package.json, if you don't see the name of the referenced module in the list, that's probably your problem too.

BaptisteB
  • 1,168
  • 2
  • 9
  • 16
0

For those who might be seeing this issue on their cicd pipelines but not on their locals. Check to see if you adjusted the casing for your file/directory names. Git didn't pick up the file renames for me.

This answer helped me fix my issue: https://stackoverflow.com/a/23609455/22479271

nick-bob
  • 1
  • 1
-1

Using this worked for me npm cache add package name@version example: npm cache add react-native-paper@0.9.1

ola korede
  • 11
  • 1
  • 1
-2

I had the exact same problem — fix was babel-preset-react-native-stage-0, instead of babel-preset-react-native.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38