87

I am getting this error after running my App:

error: bundling failed: Error: Unable to resolve module react-native-safe-area-context from node_modules/react-navigation-stack/lib/module/vendor/views/Stack/StackView.js: react-native-safe-area-context could not be found within the project.

But the same thing I had done for my old demo. It worked perfectly fine.

I don't know what I am doing wrong here. Please check my code:

For installing:

  1. React Native Navigation & Gesture Handler:

npm install --save react-navigation

npm install --save react-native-gesture-handler

  1. React Native Stack:

npm install --save react-navigation-stack

App.js

import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import FirstOptionsPage from "./FirstOptionsPage";

const MainNavigator = createStackNavigator(
  {
    FirstOptions: FirstOptionsPage
  },
  {
    defaultNavigationOptions: {
      headerStyle: {
        // backgroundColor: '#28F1A6',
        elevation: 0,
        shadowOpacity: 0
      },
      headerTintColor: "#ca375e",
      headerTitleStyle: {
        fontWeight: "bold",
        color: "#161616"
      }
    }
  }
);

const App = createAppContainer(MainNavigator); // For setting Navigation Stack
export default App;

And FirstOptionsPage.js:

import React from "react";
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text,
  ScrollView,
  Switch
} from "react-native";

export default class FirstOptionsPage extends React.Component {
  static navigationOptions = {
    title: "Preferences"
  };

  constructor(props) {
    super(props);
    this.state = {
      switch1Value: false
    };
  }

  toggleSwitch1 = value => {
    this.setState({ switch1Value: value });
    console.log("Switch 1 is: " + value);
  };

  render() {
    const { navigate } = this.props.navigation;
    return (
      <SafeAreaView style={styles.mainContainerStyle}>
        <View style={styles.subContainerStyle}>
          <Text style={styles.subtitleTextStyle}>Someone likes my post</Text>
          <View style={styles.switchStyle}>
            <Switch
              onValueChange={this.toggleSwitch1}
              value={this.state.switch1Value}
              thumbColor={MAGENTA_COLOR_CODE}
              trackColor={{
                false: GREY_COLOR_CODE,
                true: DARK_GREY_COLOR_CODE
              }}
            />
          </View>
        </View>
      </SafeAreaView>
    );
  }
}

I am new to React-Native. How can I fix this?

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Gautam Shrivastav
  • 1,198
  • 1
  • 9
  • 22
  • 2
    check for `react-native-safe-area-context` in your node modules, `react-navigation-stack` requires that but your node modules do not have that – Jigar Shah Jan 02 '20 at 08:32
  • I had to add `react-native-safe-area-context` to my `package.json` file. I don't understand why this isn't a peer dependency of React Navigation. But, maybe due to version conflict or something. If it needs it, it should be a peer dependency. – Joshua Pinter Sep 13 '21 at 13:43

13 Answers13

85

I think the problem is in the SafeAreaView, for the new react-native version, it has migrated to react-native-community/react-native-safe-area-view. if you want to use SafeAreaView, you should install it first.

the new use is like this:

import SafeAreaView from 'react-native-safe-area-view';

export default function MyAwesomeApp() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1 }}>
        <Text>
          Look, I'm safe! Not under a status bar or notch or home indicator or
          anything! Very cool
        </Text>
      </View>
    </SafeAreaView>
  );
}

for installing it you can use the following commands:
yarn add react-native-safe-area-view react-native-safe-area-context.

if you do not use auto-link, you have to also link it. for details about it, see this link

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Lenoarod
  • 3,441
  • 14
  • 25
  • 1
    Now I am getting this error ```error: bundling failed: Error: Unable to resolve module `@react-native-community/masked-view` from `node_modules/react-navigation-stack/lib/module/vendor/views/MaskedView.js`: @react-native-community/masked-view could not be found within the project.``` – Gautam Shrivastav Jan 02 '20 at 09:26
  • And actually everything was working fine in my previous project. Now it is showing this issue. – Gautam Shrivastav Jan 02 '20 at 09:27
  • you should know since react-native 0.6, many libraries had removed form react-native core. this problem like pre problem, read this link[https://github.com/react-native-community/react-native-masked-view] – Lenoarod Jan 02 '20 at 10:18
  • 9
    Well after doing what you said in your answer I also had to install ```@react-native-community/masked-view``` to fix it. Then my code worked successfully. Thanks for the help. – Gautam Shrivastav Jan 02 '20 at 11:45
  • 1
    Yeah, this is written in the release notes https://github.com/react-navigation/stack/releases/tag/v2.0.1 – mexique1 Jan 03 '20 at 16:34
47

It is a little funny, I wanted to add navigation so I added

npm install --save react-navigation

for this to work fine I had to add: expo install react-native-gesture-handler react-native-reanimated

Then I got

Unable to resolve "react-native-safe-area-context" So I added:

expo install react-navigation-stack

expo install react-native-safe-area-view react-native-safe-area-context

then I got

bundling failed: Error: Unable to resolve module @react-native-community/masked-view`

So I searched for the masked-view (which currently is not supported by the expo, according to its git document). Then I found out that I can use:

expo install @react-native-community/masked-view, which could work or not :)

Therefore, from now on I use following commands at the start of all of my react-native projects, to be able to use navigation properly:

npm install --save react-navigation

expo install react-native-gesture-handler react-native-reanimated react-navigation-stack

expo install react-native-safe-area-view react-native-safe-area-context

expo install @react-native-community/masked-view.

Pranav J
  • 82
  • 6
Arash Rabiee
  • 1,019
  • 2
  • 16
  • 31
23

After running these commands:

npm i react-native-safe-area-view react-native-safe-area-context &&
react-native link react-native-safe-area-context

It prompted me an error about masked-view, so I also had to run npm i @react-native-community/masked-view and then my code can now be successfully run on Android physical device.

Thanks to Lenoarod and Gautam Shrivastav for pointing out the right direction.

Antonio Jack
  • 241
  • 1
  • 3
15

installing following two,

npm install --save @react-native-community/masked-view
npm install react-native-safe-area-context

it is work for me

Kaumadie Kariyawasam
  • 1,232
  • 3
  • 17
  • 35
  • 1
    I don't understand why this is not in dependencies list of react-navigation. Anyways installing these two worked for me. – Ali Rehman Jun 24 '20 at 22:35
6

I think you miss link depency with your project so you can try as below:

With React Native 0.6 or higher:

On iOS, make sure you have Cocoapods installed and run:

cd ios
pod install
cd ..

With React native 0.59 and lower try:

react-native link react-native-safe-area-context
5

copy all and paste in terminal

expo install react-navigation react-native-gesture-handler react-native-reanimated react-native-screens

work for me

3

Run the following:

expo install react-native-safe-area-context

expo will select the right version and then install it.

Joey Smith
  • 31
  • 1
3

To use React Navigation you need to run the following command

npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

or

yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
Dark Matter
  • 373
  • 6
  • 18
1

i did this yarn add @react-native-community/masked-view yarn add react-native-safe-area-context and it gave me another error on import type { ScreenProps } from 'react-native-screens'

then did yarn add react-native-screens all went well

Drweb12
  • 19
  • 4
0

use the commend npm i react-navigation-stack t solve this error

Umer Javed
  • 39
  • 1
  • 5
0

Starting the Metro Bundler directly from the project directory works for me.

# Clean cache rm -rf $TMPDIR/react-*; rm -rf $TMPDIR/haste-*; rm -rf $TMPDIR/metro-*; watchman watch-del-all

# Start Metro Bundler directly react-native start

# Now run react-native run-android or react-native run-ios in another tab

Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

If you are using @react-native/stack then before installing it use following command to install it's dependency

npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

read the documentation at https://reactnavigation.org/docs/getting-started/

Er. Mohit Agrawal
  • 2,116
  • 1
  • 17
  • 15
0

Me I think it is due to incompatible version pairs for expo and safe area context. You should try run this:

npm uninstall react-native-safe-area-context

After, you run this :

expo install react-native-safe-area-context

  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 07 '21 at 06:43