I installed react-native, but cannot find module

Viewed 2249

From my command line, I ran

npm install react-native

In my package.json:

"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-modal": "^3.11.2",
"react-native": "^0.62.2",
"react-native-cli": "^2.0.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1"

In my package-lock.json there are plenty of entries for react-native.

But when I try to do

import React, {Component} from 'react';
import { View, Text, Image, ScrollView } from 'react-native';
...
return <View>foo</View>

And run

npm run-script build

I get

Creating an optimized production build...
Failed to compile.

./src/components/main.component.js
Cannot find module: 'react-native'. Make sure this package is installed.

You can install this package by running: npm install react-native.

The only hint is when I do npm install I get

npm WARN react-native@0.62.2 requires a peer of react@16.11.0 but none is installed. You must install peer dependencies yourself.

but when I changed react to 16.11.0, and react-dom, it just went back to the same error.

1 Answers

You need to install the CLI globally, also i recommend on the community cli @react-native-community/cli which is actually maintained.

Run npm install -g @react-native-community/cli, then npx react-native init AppName with the globally installed cli. Don't forget to delete the obsolete cli package.

Related