Unable to Resolve Module in React Native App

React Native

React Native Problem Overview


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!

React Native Solutions


Solution 1 - React Native

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

Solution 2 - React Native

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

Solution 3 - React Native

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!

Solution 4 - React Native

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

Solution 5 - React Native

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.

Solution 6 - React Native

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

Solution 7 - React Native

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

Solution 8 - React Native

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

Solution 9 - React Native

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.

Solution 10 - React Native

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

Solution 11 - React Native

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

Example:

npm cache add react-native-paper@0.1.9

Solution 12 - React Native

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

Solution 13 - React Native

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.

Solution 14 - React Native

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'

Solution 15 - React Native

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.

Solution 16 - React Native

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

Solution 17 - React Native

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.

Solution 18 - React Native

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

Solution 19 - React Native

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

Solution 20 - React Native

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.

Solution 21 - React Native

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.

Solution 22 - React Native

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.

Solution 23 - React Native

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

Solution 24 - React Native

Using this worked for me npm cache add package name@version example: npm cache add [email protected]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxwellgoverView Question on Stackoverflow
Solution 1 - React NativemaxwellgoverView Answer on Stackoverflow
Solution 2 - React NativePablo DardeView Answer on Stackoverflow
Solution 3 - React NativeDariusView Answer on Stackoverflow
Solution 4 - React Nativemax23_View Answer on Stackoverflow
Solution 5 - React NativeOvercomerView Answer on Stackoverflow
Solution 6 - React Nativeillya yurkevichView Answer on Stackoverflow
Solution 7 - React Nativeashwath hegdeView Answer on Stackoverflow
Solution 8 - React NativeBrijesh ShiroyaView Answer on Stackoverflow
Solution 9 - React NativeAamir Ali HussainView Answer on Stackoverflow
Solution 10 - React NativeNirav PatelView Answer on Stackoverflow
Solution 11 - React Nativeola koredeView Answer on Stackoverflow
Solution 12 - React NativeMatt AftView Answer on Stackoverflow
Solution 13 - React NativeGrez.KevView Answer on Stackoverflow
Solution 14 - React NativeFaris RayhanView Answer on Stackoverflow
Solution 15 - React NativeLucas AraujoView Answer on Stackoverflow
Solution 16 - React NativechrisView Answer on Stackoverflow
Solution 17 - React NativeStuart AitkenView Answer on Stackoverflow
Solution 18 - React NativeHaseeb AView Answer on Stackoverflow
Solution 19 - React NativeAlaa AbdelfattahView Answer on Stackoverflow
Solution 20 - React NativeStephenView Answer on Stackoverflow
Solution 21 - React NativeHimanshu Singh ChauhanView Answer on Stackoverflow
Solution 22 - React NativefixedDrillView Answer on Stackoverflow
Solution 23 - React NativeAndrew MontagueView Answer on Stackoverflow
Solution 24 - React Nativeola koredeView Answer on Stackoverflow