Creating new component fails: Unable to resolve module - react-native

I created a fresh react native project (version 0.47.1). Now I'm trying to write a new component for my react native app.
/my-component.jsx
import React from 'react';
import {Text} from 'react-native';
export default class MyComponent extends React.Component {
render() {
return <Text>My Component</Text>
}
}
/index.android.js
import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import MyComponent from './my-component';
export default class Test extends Component {
render() {
return (
<MyComponent />
);
}
}
AppRegistry.registerComponent('test', () => Test);
The packager throws the following error:
error: bundling failed: "Unable to resolve module `./my-component` from `<project_root_dir>/index.android.js`:
could not resolve `<project_root_dir>/my-component' as a folder: it did not contain a package, nor an index file"
Adding the file extension to the import results in the same error, except with the file extension appended to the paths in the error.
The same error goes for dependencies added via npm. In any case the path shown in the error exists.

It seems like you have moved the entry file i.e index.android.js into a custom directory i.e test => (test/index.android.js). For this purpose it cannot locate the entry file. Placing the index.android.js again into the root directory will solve the problem.
If you want to change the entry point of the project you can refer the following:
https://github.com/facebook/react-native/issues/3442
Change entry file path of android and ios in react-native project

Related

Unable to resolve module 'react-redux'...module 'react-redux' does not exist in haste module map

The code in app.js file with import statements for react redux is created to display a header with a text called "Tech stack".
App.js
import React from 'react';
import { View } from 'react-native';
import { Header } from './components/common';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from './reducers';
const App = () => {
return(
<Provider store={createStore(reducers)}>
<View>
<Header headerText="Tech Stack" />
</View>
</Provider>
);
};
export default App;
This is the index file
index.js
import { AppRegistry } from 'react-native';
import App from './src/App';
AppRegistry.registerComponent(tech_stack, () => App);
While running this on the terminal, it throws a error saying unable to resolve module 'react-redux'.
Close the JS bundle(a terminal starts when you run app for first time) and rerun it using the command react-native start from the project path. Basically, you need to rerun the JS bundle after every package installation.
Install 'react-redux' by using
npm install --save react-redux
if you not installed it. By seeing your comment as you mentioned that you installed redux only.
Then restart the JS bundle by using
react-native start --reset-cache
I've faced this issue just now. actually, Our (you and I) main issue is naming the global state folder name is redux and the bundler falls in a conflict because there is a folder inside node_modules that name is redux too.
In fact, the main issue is this line:
import { createStore } from 'redux';
I renamed the resux stuffs folder to reduxStore instead of redux and then everything works properly.
This error occurs because you do not have Redux-Thunk middleware installed on your application. To install run:
npm install redux-thunk
Then, restart your application:
react-native start
More information: https://github.com/reduxjs/redux-thunk
I just ran into this problem and solved it by installing redux in addition to react-redux. Apparently, react-redux requires it.

import Swiper show error by using react-native-swiper

I try to use swiper from this library https://github.com/leecade/react-native-swiper
Here is my dependencies:
"react": 16.3.1,
"react-native": "~0.55.2",
"react-native-swiper": "^1.5.13"
But when i just add this code import Swiper from 'react-native-swiper';
My component like:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Swiper from 'react-native-swiper';
class Welcome extends Component {
render() {
return (
<View>
<Text>Welcome</Text>
</View>
);
}
}
export default Welcome;
It will shows error even i don't use the <Swiper />
error:
Failed to load bundle
Check my terminal error shows:
error: bundling failed: Error: While trying to resolve module `react-native-swiper` from file `/Users/huaweb/ReactNativeProject/Huaweb/src/components/Welcome.js`, the package `/Users/huaweb/ReactNativeProject/Huaweb/node_modules/react-native-swiper/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/huaweb/ReactNativeProject/Huaweb/node_modules/react-native-swiper/index.js`. Indeed, none of these files exist:
I don't know what is However, this package itself specifies amainmodule field that could not be resolved
I can't figure it out. Is any thing i miss it ?
Any help would be appreciated, thanks in advance.
In node_modules find the folder react-native-swiper and do so, It worked for me
replace :
module.exports = Swiper;
module.exports.default = Swiper;
by
export default Swiper
export {Swiper}
try with:
import Swiper from 'react-native-swiper/src';

How to define entry point for react native app

I'm just getting started with react native and have created a base app with create-react-native-app.
I did some restructuring and made a few new folders and renamed the App.js to Home.js. I modified the app.json to contain an entry point that references the new Home.js file. When I load the app, nothing happens. There's no error, it just stays on the expo screen.
.
-components
-screens
-Home
Home.js
-config
-node_modules
-tests
app.json
app.json file:
{
"expo": {
"sdkVersion" : "23.0.0",
"entryPoint" : "./screens/Home/Home.js"
}
}
How do you define the entry point of the app?
if you are using Expo, you have to specify the entrypoint in your app.json file like this:
{
"expo": {
"entryPoint": "./src/app/index.js"
}
}
then, inside that file you need to register the app with Expo.registerRootComponent(YOUR_MAIN_APP_COMPONENT)
import Expo from 'expo'
...
class App extends Component {
...
}
export default Expo.registerRootComponent(App);
this way you can add your entry file wherever you want.
You need to update the app.json so that the entryPoint is the new path to the App.js.
{
"expo": {
"entryPoint": "./src/App.js",
...
}
}
However using Expo.registerRootComponent(App) causes the following error in SDK 32:
undefined is not an object (evaluating '_expo.default.registerRootComponent')
It can be fixed by importing registerRootComponent explicitly, rather than trying to access it via Expo.registerRootComponent.
Here is a sample App.js.
import { registerRootComponent } from 'expo';
class App extends React.Component {
...
}
export default registerRootComponent(App);
For Expo Projects
According to the current Expo documentation, if you want a different entry point than the App.js file, you can update the package.json - add a main field with the path to the desired entry point. Then inside the entry point file you'll have to also have to register the root component of the app. Expo was doing this automatically, when the entry point wasn't specified and was the App.js file
package.json
{
"main": "my/customEntry.js"
}
entryPointFile.js
import { registerRootComponent } from 'expo';
import MyRootComponent from './MyRoot';
registerRootComponent(MyRootComponent);
What if I want to name my main app file something other than App.js? - https://docs.expo.io/versions/latest/sdk/register-root-component/#what-if-i-want-to-name-my
If your project is in managed workflow setup (the default one), as stated in the doc, you must import the registerRootComponent and call it with your root component as argument, in the file you wish to be the main one:
import { registerRootComponent } from 'expo';
const AnyName() { ... } // Your root component
registerRootComponent(AnyName)
And then, in your package.json file, change the "main" to this file relative path, like
{
"main": "src/main.js"
}
I created project by react-native-script. In default entrypoint of app (App.js), you export App which import from your entry.
- node_modules
- App.js
- build
- main.js
File App.js:
import App from './build/main'
export default App
I also prefer to put all sources in a separated folder, for instance src/, and I found a different solution:
in my package.json, generated by expo cli, I see that main attribute is node_modules/expo/AppEntry.js.
I copied node_modules/expo/AppEntry.js to src/expoAppEntry.js and just changed the App import to import App from './App'; so it points to my *src/App.tsx`
then of course I changed the package.json main attribute to src/expoAppEntry.js.
See a working example here https://github.com/fibo/tris3d-app/blob/master/src/expoAppEntry.js
For those who are using Expo with typescript, you dont have to add .tsx at the end of the entrypoint in app.json. For example your entrypoint can be:
{
"expo": {
"entryPoint": "./app/components/AppEntryPoint/App.component",
"name": "Sample App",
...
}
...
}
In this example the name of entrypoint component is App.Component.tsx. But not mentioning the extension will also work. Apart from this, in the root component, writing export default registerRootComponent(AppComponent) or registerRootComponent(AppComponent) both should work as exporting a component from a file only means that other files can use it as well. Not writing it should not be an issue here because we have mentioned in app.json that this is the root component. App.json will look up and start building the structure of the app from there itself.
The entry point can be found in node_modules/expo/AppEntry.js..
This is in Expo Typescript...
import registerRootComponent from 'expo/build/launch/registerRootComponent';
import App from '../../src/App';
registerRootComponent(App);
In this you can change your entry point. Initially it is set to App, Look the import statement where that component is coming from.

Cannot find entry file index.android.js in any of the roots

I am using react-native version 0.49.3 and I have created a file index.js in my project directory. it works for ios but when I run it for Android, it gives error
Cannot find entry file index.android.js in any of the roots
Do I need to add something to my android folder?
The default React Native's starter included two separated files call index.ios.js and index.android.js.
If you want to combine root files -
Create one files (index.base.js, index.app.js, index.root.js, base, etc)
Register your component using AppRegistry.registerComponent in your created files.
Import created file in each separated files.
Example
/**
* root.js
*/
import React, { Component } from 'react'
import { AppRegistry } from 'react-native'
class AppName extends Component {
...
}
AppRegistry.registerComponent('AppName', () => AppName )
/**
* index.android.js and index.ios.js
*/
import './root'

"Module does not exist in the module map or in these directories" in react native

I have started learning react native. I am struggling in the simple react native project while importing new js [example Home.js] in index.android.js
I'm getting the following error"
Module does not exist in the module map or in these directories.
UnableToResolveError: Unable to resolve module .src/Components/home/Home/Home.js from C:\Users\Magi\Dictionary1\index.android.js: Module does not exist in the module map or in these directories:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import Home from '.src/Components/home/Home/Home.js';
Some unwanted import can be causing the issue.
For me, this state was creating the issue.
import { exec } from "child_process";
If Home.js and index.android.js are in the same directory, you can replace
import Home from '.src/Components/home/Home/Home.js';
with
import Home from './Home.js';
But if your src folder is at the same level as index.android.js, src needs to be preceded by ./ instead of .:
import Home from './src/Components/home/Home/Home.js';