question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

importing throws error unless index is specified

See original GitHub issue

Hi,

This is weird…

I have a file app/index.js with:

import React from 'react';
import { View, Text } from 'react-native';
export const App = () => (
  <View>
    <Text>The App</Text>
  </View>
);
export default App;

In this case I should be able to do either import App from './app' or import { App } from './app' and use it in my AppRegistry.registerComponent.

However I get the following error unless I import the path ./app/index which you should not need to do.

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

Versions installed, osx siera, node v7.5.0:

react-native-cli: 2.0.1
react-native: 0.41.2

This is the first I have ever come across having to specify index is this a known issue?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

15reactions
rhysburniecommented, Feb 27, 2017

@brentvatne FYI - Figured out what was happening…

If you have a file ./app/index.js and you attempt to import like so:

import Something from './app';

What actually happens is the system is importing the root level ./app.json file which contains the object in question.

So while this isn’t a bug it is perhaps a gotcha?

It seems reasonable to expect that some developers may create an app folder and that they may create an index.js in that folder and expect it to import as expected.

The gotcha is that the system prefers ./app.json over ./app/index.js

2reactions
ujwal-setlurcommented, Aug 3, 2017

Got burned by this as well 😃. Interestingly, it worked fine on android, but gave problems on ios. I just deleted app.json. Is it actually needed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when styling React components imported from index file ...
If I import the component directly from the source file I do not get the error. It only happens when I try to...
Read more >
Import - Oracle Help Center
2. Import. This chapter describes how to use the Import utility, which reads an export file into an Oracle database. Import reads only...
Read more >
Getting 'Invalid Index' error in Projectwise Import Tool
I'm trying to import an excel sheet file using the PW import tool but during the second step I'm getting 'Invalid Index' error...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
js file containing the module. In Node, extension-less imports often refer to packages in node_modules . Certain bundlers may permit importing ...
Read more >
How to use an ES6 import in Node.js? - GeeksforGeeks
If we try to use import for importing modules directly in node js it will throw out the error. For example, if we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found