I recently updated my app to enforce ES6 by adding "type:module" to my package.json. In addition, I changed the following to accommodate this change.
next.config.js -> next.config.cjs
tailwind.config.js -> tailwind.config.cjs
postcss.config.js -> postcss.config.cjs
The problem is since doing so, I get the following error on all of my Image components :
Warning: React.jsx: 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.
Example of the Image component I’m using to render
<Image
src="/next.svg"
fill
style={{
objectFit: "contain",
}}
alt="Your Company"
/>
It’s imported like import Image from "next/image";
If I remove all of the Image components from my app, it will load fine. It’s important to note I never had an issue before enforcing ES6.
2
Answers
It turns out that this was an issue with the NextJS Image component. Currently, it seems like the Image component does not support ES Modules. As a result, I was getting the above error.
Instead of importing it like
import Image from "next/image";
import it asYou can find more detail on this issue at https://github.com/vercel/next.js/issues/54777
The error or warning you encountered is typically related to mistakes in import/export, such as using an incorrect path or using a variable as a JSX component. Ensure that you use the correct path and follow proper import/export conventions to avoid this issue.
( Check Your Image Component Is this Component Import/export Correct? Read all this and simply use Image Component )
1. Mixing up default and named imports and exports:-
When we export default, mean only one component export then use
import simple_component
name. If export component_named thenimport {Component_named}
Example :-
Header.js
App.js
2. Declared a variable with JSX code instead of a function
The below code will throw the error.
However, the Message variable cannot be used as a component unless it is not a function. React components should be a function or class that returns HTML. To solve the error, change the Message variable to a function that returns JSX code as given below.