user2364424
user2364424

Reputation: 101

Realm and React error. Can't resolve 'react-native' in app created with create-react-app

I'm attempting to add the Realm package to a project created with create-react-app. I simply run the create script, install the project, then install realm. When I attempt to import or require 'realm' I get a Can't resolve 'react-native' error.

npx create-react-app my-app
cd my-app
npm i
npm i --save realm
npm start

At this point, if I attempt to import realm anywhere in the project, for instance by adding:

import Realm from 'realm';

or

Realm = require('realm');

to App.js or Index.js

I get the following error.

./node_modules/realm/lib/browser/index.js
Module not found: Can't resolve 'react-native' in '/Users/scolobey/Desktop/Projects/realm-blog/node_modules/realm/lib/browser'

As far as I have been able to tell, this seems to be related to webpack. Opening the console on this error page, I see:

./node_modules/realm/lib/browser/index.js
Module not found: Can't resolve 'react-native' in '/Users/scolobey/Desktop/Projects/javascript/my-app/node_modules/realm/lib/browser'
console.<computed> @ index.js:1
r @ backend.js:6
handleErrors @ webpackHotDevClient.js:173
push../node_modules/react-dev-utils/webpackHotDevClient.js.connection.onmessage @ webpackHotDevClient.js:212

Upvotes: 0

Views: 1597

Answers (1)

JBallin
JBallin

Reputation: 9787

Create React App is for regular React apps (not mobile/“React Native” apps). Your app is complaining because it expects to find the “react-native” package, which doesn’t come included with Create React App.

You’ll note that Realm’s Installation Guide specifically instructs you to set up react native before installing the package.

As mentioned in the React Native Docs - Getting Started (which is linked to in the above Realm installation guide), you likely want to use expo instead of Create React App.

Once the React Native app is set up, you can proceed with installing Realm.

Upvotes: 2

Related Questions