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.

Import order wrongly causing "Element type is invalid" errors in stories

See original GitHub issue

Description

When using a component import/export entry-point for terse imports throughout the application the order of imports can cause unexpected errors in Storybook only; not in the yarn start environment or compiled application. The error is:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Imagine we have a components/ directory, in which we have an import/export entry-point index.js and each component has its own directory A/ and B/.

src/components/index.js:

// This order causes no errors in `yarn start` but causes an error in the story for component A
import { A } from "./A";
import { B } from "./B";
// This order causes no errors anywhere
// import { B } from "./B";
// import { A } from "./A";

// It doesn't matter in which order we export (A,B or B,A), only the order of the import causes the error
export { A, B };

This behaviour only appears to affect a component if it itself imports another component which appears below itself in the import/export entry-point.

For example in the above example component A itself imports component B to use within its render, and the only way to avoid the error in the story for component A is to remove component B from it or to swap the order of the imports in src/components/index.js.

Reproduction repo below for simpler demonstration.

To Reproduce

I understand that the description may be a little confusing to follow so I have created a barebones CRA + Storybook + 2 components reproduction repository which also causes the error.

Reproduction: https://github.com/chrisdunnbirch/storybook-import-bug

  1. Clone git@github.com:chrisdunnbirch/storybook-import-bug.git
  2. Run yarn install
  3. Run yarn storybook
  4. Check A > Story in Storybook (should render with above error) a. Run yarn start (should render as expected: “AB”)
  5. Comment lines 4-5 and uncomment lines 8-9 in src/components/index.js
  6. Check A > Story in Storybook (should render as expected: “AB”)

Expected behavior

The story renders without error, as expected based on the behaviour of the yarn start environment and compiled application.

System

I first noticed this in a large TypeScript project which at the time had 5.3.0-rc.10 installed and the issue is still present in 5.3.6. The example reproduction repo above is the absolute barebones CRA + Storybook + 2 components with no TypeScript.

Additional context

It is worth noting that I have added a .env with NODE_PATH=src to recreate the behaviour of absolute imports that the CRA TypeScript preset allows by default.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:35 (11 by maintainers)

github_iconTop GitHub Comments

19reactions
Jack-Workscommented, Sep 15, 2020

Also having this problem on Storybook 6

6reactions
GLoschcommented, Mar 31, 2021

storybook version 6.2.1 resolves this issue for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Element type is invalid: expected a string (for built-in ...
In my case I resolved the problem by removing undefined component. I imported that component from wrong module.
Read more >
Element type is invalid: expected a string (for built-in ... - Reddit
Really annoying/frustrating issue but it's usually linked to imports and exports. You just have to read the import and export lines one by...
Read more >
Error: Element type is invalid - The freeCodeCamp Forum
It can be caused by a number of things. The error message gives you two possible suggestions. Without more of the code, it's...
Read more >
Fixing common type problems - Dart
Fixing common type problems · Undefined member · Invalid method override · Missing type arguments · Unexpected collection element type · Constructor initialization ......
Read more >
uncaught error: element type is invalid. received a promise ...
To fix this error, you will need to ensure that the imported module has a default export that resolves to a React component....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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