80

I am new to react-native but not ReactJs iam going to mad about this error from 2 days

error: bundling failed: Error: While trying to resolve module `@react-navigation/native` from file `C:\XXXXX\Example\src\Router.jsx`, the package `C:\XXXXX\Example\node_modules\@react-navigation\native\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\XXXXX\Example\node_modules\@react-navigation\native\src\index.tsx`. Indeed, none of these files exist:

error: bundling failed: Error: While trying to resolve module `react-native-safe-area-context` from file `C:\XXXXX\Example\App.js`, the package `C:\XXXXX\Example\node_modules\react-native-safe-area-context\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\XXXXX\Example\node_modules\react-native-safe-area-context\src\index.tsx`. Indeed, none of these files exist:

This is my package.json

{
  "name": "Example",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.6",
    "@react-navigation/native": "^5.0.0",
    "@react-navigation/stack": "^5.0.0",
    "eslint": "^6.8.0",
    "jetifier": "^1.6.5",
    "native-base": "^2.13.8",
    "prop-types": "^15.7.2",
    "react": "16.8.6",
    "react-native": "0.60.0",
    "react-native-camera": "^3.16.0",
    "react-native-gesture-handler": "^1.5.6",
    "react-native-image-crop-picker": "^0.28.0",
    "react-native-reanimated": "^1.7.0",
    "react-native-safe-area-context": "^0.7.2",
    "react-native-safe-area-view": "^1.0.0",
    "react-native-screens": "^2.0.0-beta.2",
    "react-native-svg": "^11.0.1",
    "react-native-vector-icons": "^6.6.0"
  },
  "devDependencies": {
    "@babel/core": "^7.3.3",
    "@babel/runtime": "^7.3.1",
    "@react-native-community/eslint-config": "^0.0.3",
    "babel-jest": "^24.1.0",
    "jest": "^24.1.0",
    "metro-react-native-babel-preset": "^0.54.1",
    "react-test-renderer": "16.8.6"
  },
  "jest": {
    "preset": "react-native"
  }
}

I am downgrade the react-navigation version to react-navigation 4 and react-navigation-stack the error become

error: bundling failed: Error: While trying to resolve module `react-native-safe-area-context` from file `C:\XXXXX\Example\node_modules\react-navigation-stack\lib\module\vendor\views\Stack\StackView.js`, the package `C:\XXXXX\Example\node_modules\react-native-safe-area-context\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\XXXXX\Example\node_modules\react-native-safe-area-context\src\index.tsx`. Indeed, none of these files exist:

I am also deleted node_modules ,clear cache and install again but no use same errors appear

Jogi Prasad Pakki
  • 2,013
  • 2
  • 11
  • 21
  • The answers presented above didn't work for me. I had to [clear the cache of my React Native Project](https://stackoverflow.com/a/51375437/8577483) – Sami Oct 21 '21 at 23:05

19 Answers19

110

After a long research MetroJS bundler default not compile typescript .ts and .tsx extension files. We need tell MetroJS bundler to compile .ts and .tsx files i solved this error by edit metro.config.js file in root project folder by following.

Edited on September 2022

  • Added cjx and json extensions to below snippet
  • All extensions listed below not required it's depend on what language you choose javascript or typescript, and your dependencies

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  resolver: {
    sourceExts: ['jsx', 'js', 'ts', 'tsx', 'cjs', 'json'] //add here
  },
};
Jogi Prasad Pakki
  • 2,013
  • 2
  • 11
  • 21
50

The above answers did not work for me. Turns out there was nothing wrong with my files.

I simply had to terminate the react-native metro that was running on the terminal and restarted it, it worked fine.

Harsha
  • 1,568
  • 2
  • 19
  • 22
22

Adding to the accepted answer by Jogi, this issue was acknowledged by the Apollo developers at https://github.com/apollographql/apollo-client/blob/main/CHANGELOG.md#apollo-client-354-2021-11-19

I was unable to resolve the issue with Jogi's answer alone, but after adding 'cjs' to the array, as recommended in the linked changelog, my app was able to start up as expected.

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
  resolver: {
    sourceExts: ['jsx', 'js', 'ts', 'tsx', 'cjs'],
  },
};
Byron
  • 1,091
  • 11
  • 27
Robin Dowling
  • 360
  • 4
  • 9
11

For me, I'm using two terminals to run my app. 1st terminal: react-native start, 2nd: react-native run android, which builds the app and starts it on my physical device. I got the same error above when I installed a new package and rebuild my app not knowing that I had to restart the react-native server.

So what I just did is went back to 1st terminal, killed it, and re-run react-native start then rebuilt my app in the second terminal and everything is working as expected!!!

Shamxeed
  • 382
  • 4
  • 8
3

here is how my metro.config.js it looks like after configuring. i am using react-navigato 5*

    module.exports = {
        transformer: {
          getTransformOptions: async () => ({
             transform: {
                experimentalImportSupport: false,
                inlineRequires: false,
             },
           }),
         },
          /** include this below (remove this comment too)*/
       resolver: {                              
         sourceExts: ['jsx', 'js', 'ts', 'tsx'],
       },
   };
Slycreator
  • 1,144
  • 11
  • 18
3

I got the same error with react-native-gesture-handler package.Changing metro.config.js file has worked for me.

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

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },

  resolver: {
    sourceExts: ['jsx', 'js', 'ts', 'tsx'], 
  },
};
MertStack
  • 51
  • 2
3

As many of them already mentioned to customize your metro.config.js file. If it is not there create it in the root directory of your project and then add the code as everyone said. Now even after adding the code in metro.config.js file error is there, then you have to stop your expo server and restart with command

expo start -c

This works for me. Hope it will help you too.

2

This has worked for me.

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

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  
  resolver: {
    sourceExts: ['jsx','js','ts','tsx'] //add here 
   },
};
2

I had to clear watchman to resolve a similar issue

watchman watch-del-all
sushrut619
  • 843
  • 8
  • 20
1

For Expo, this version worked https://github.com/firebase/firebase-js-sdk/issues/6253#issuecomment-1123923581

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

const defaultConfig = getDefaultConfig(__dirname);

defaultConfig.resolver.assetExts.push("cjs");

module.exports = defaultConfig;
Thomas Hagström
  • 4,371
  • 1
  • 21
  • 27
0

Sometimes, you get this error because you install an npm package that has a main script that's not correctly integrated with your project. This could be because you have named your files differently, you have nested your file a layer deeper and causes a script to break, etc. Check the package.json file of the package the error message tells you "main" is incorrect and see if it is correctly pointing to the app entry in the package.json of the root directory. Doing this fixed the error for me.

0

for me none of the above worked but this did: go to : node_modules\deprecated-react-native-prop-types\DeprecatedColorPropType.js

find this line : const normalizeColor = require('@react-native/normalize-color'); and change it to this: const normalizeColor = require('@react-native/normalize-color/base');

Muhammad Muaz
  • 157
  • 1
  • 13
0

I tried all the solutions above but none worked. so i did yarn install instead of npm and it worked

vvspower
  • 49
  • 1
  • 6
0

For me, It was to kill my metro cmd

Deleted my app from the emulator then,

run npx react-native run-android or

if you're using expo run

npm start

Error is gone

vimuth
  • 5,064
  • 33
  • 79
  • 116
0

kill the already running terminal and start the project by running this command: npx react-native start it will start your app as expected

0

I got the same issue.I am using yarn as my package manager. Resolved the issue by following the below steps.

  • Stop the metro bundler(In my case i closed terminal which terminated metro bundler).
  • Restart it using yarn start --reset-cache.
  • Run yarn android.
Gavara.Suneel
  • 458
  • 2
  • 14
0

I ran into this error when trying to load @angular/core. You carefully need to check your error log (as usual ;)). Apparently there are way more file extensions you should add to your metro.config.js, according to the module you are using.

@angular/core e.g. uses d.ts (because it's index.d.ts in the module's folder) so I needed to add it to the array:

  sourceExts: ['jsx', 'js', 'ts', 'tsx', 'cjs', 'json', 'd.ts'] 

After that, of course, a fresh restart is recommended

npx react-native start --reset-cache
n.r.
  • 831
  • 1
  • 11
  • 30
0

Restart worked for me . just close the terminal and run your project again with npx react-native run-android

Kashinath
  • 194
  • 1
  • 1
  • 8
-1

In my case the error was due to the cache. I followed following steps:

Navigate to android folder

cd android

after that clean the build folder

 ./gradlew clean

and to generate the apk file

./gradlew assembleRelease -x bundleReleaseJsAndAssets

use this to generate aab

./gradlew bundleRelease -x bundleReleaseJsAndAssets

I hope these commands will help others as well.

surhidamatya
  • 2,419
  • 32
  • 56