75

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!

4
  • 1
    Once I made a file called Index.js but imported index.js. Hello Case-sensitive-imports, my old friend. Commented Jan 8, 2021 at 13:31
  • Hi , this was 2016 but i have same error, if you know answer pls help. Commented Jan 17, 2022 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. Commented Feb 25, 2023 at 7:06
  • 1
    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: stackoverflow.com/a/23609455/22479271 Commented Aug 31, 2023 at 17:11

29 Answers 29

201

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

Sign up to request clarification or add additional context in comments.

Comments

24

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

5 Comments

I wouldn't even use my components because I was using .jsx, does that mean React Native does not support jsx files?
Yes, I believe React native doesn't have support to JSX files.
@GilbertR., I built an app with pure React Native, and it doesn't support .jsx files too. You can check it here
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
I can't understand why .jsx won't be valid file extension for a react-native filename.
16

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!

Comments

10

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

2 Comments

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
this is the only one that worked for me. I only needed rm -rf ${TMPDIR:-/tmp}/metro-*
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.

Comments

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

9 Comments

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!
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.
@staxwell In which file you are actually importing the AppContainer? Since you are saying export is done in app/containers/index.js.
exporting is done in app/containers/index.js importing is done in app/index.js
@staxwell Oh, can you try this: import { AppContainer } from './containers/index';
|
2

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.

Comments

2

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

Comments

2

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

1 Comment

React-native does automatic linking from 0.60 version onwards.
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';

1 Comment

Do you know why this would work?
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.

Comments

1

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

Comments

1

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

Comments

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

3 Comments

Hi! Try to add some more explaination to your solution
I get zsh: no matches found: /tmp/metro-bundler-cache-* did it change?
Metro Bundler cache can now be found here: rm -rf ${TMPDIR:-/tmp}/metro-*. Source: facebook.github.io/metro/docs/troubleshooting
1

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

Example:

npm cache add [email protected]

Comments

1

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

npm install

Hopefully this fixed it.

Comments

1

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.

Comments

1

See the image of error log

Here is the Answer of quations : "Module "62" is missing from the asset registry" in react-native and react-native-web

Run this command in your terminal: "yarn expo install --fix"

Comments

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'

Comments

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.

Comments

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.

Comments

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

15 Comments

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.
It says it can't resolve module ./containers when I try that.
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.
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!!
np, can you post a ss of your app/index.js?
|
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.

Comments

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.

Comments

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.

1 Comment

@Tuzi Check whether my answer helps
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.

Comments

0

I was having a different reason. Documenting it for others below.

I started to get this error, the moment I added metro.config.js to my app. I copied the contents from a tutorial as below:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

const {getDefaultConfig} = require('metro-config');

module.exports = async () => {
  const {
    resolver: {assetExts, sourceExts},
  } = await getDefaultConfig();
  return {
    resolver: {
      assetExts: assetExts.filter(ext => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg'],
    },
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
    },
  };
};

But, I had to do the following changes to make it work:

  • change the require path to expo/metro-config from just metro-config
  • change the getDefaultConfig() call to getDefaultConfig(__dirname)
  • extend existing configs, instead of overriding them

Here is the updated metro.config.js that worked for me:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

const {getDefaultConfig} = require('expo/metro-config');

module.exports = async () => {
  const defaultConfig = await getDefaultConfig(__dirname);
  const {
    resolver: {assetExts, sourceExts},
  } = defaultConfig;
  
  return {
    ...defaultConfig,
    resolver: {
      ...defaultConfig.resolver,
      assetExts: assetExts.filter(ext => ext !== 'svg'),
      sourceExts: [...sourceExts, 'svg'],
    },
    transformer: {
      ...defaultConfig.transformer,
      babelTransformerPath: require.resolve('react-native-svg-transformer'),
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
    },
  };
};

Comments

-1

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

Comments

-2

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

1 Comment

Where should that line be?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.