DevOps | Cloud | Analytics | Open Source | Programming





How To Fix - "Uncaught Error: Invariant Violation: Element Type is Invalid:" ?



In this post, we will see How To Fix - "Uncaught Error: Invariant Violation: Element Type is Invalid:". Various formats of the above error -


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


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

 

Primitive Checks:

First thing first, check the below pointers to ensure you are at the right foot.

  • Are you using, importing or referencing any component is not present or imported . This may be due to any typo or often something that didn't catch attention like - version details, corresponding components names etc.
    • Incorrect import
    • Missed to to export/import something
    • Mismatched type of import (default/named)
    • Importing from a wrong library or path
  • Are you using any Styled-components ? The place of location might sometimes render some components inaccessible to other parts of the code.
  • Are you using the Import statements correctly ? And whatever is being "imported" are they correctly being pinpointed and pulled ?
It would be a Good Practice to cross-check these things once.  

Possible Fixes :

  • Did you miss any semicolon at the import-declaration statement ?

import x from './X'   <--- Throws Error 

import x from './X' **;** <--- No Error

 

  • Possible export declaration error

export default A();  <--- Throws Error 


export default A;    <--- No Error

 

  • Refer below scenario
Your export file might have the word "default".  If you are exporting something as default, then you should not import it as a named export.


export default class <THE\_NAME>

In that case, do not use {} when you import. See below example -


import { component } from "react"   <--- Throws Error 

import component from "react" <--- No Error

Or scenario


import {SOMETHING} from '../SOMEJS.js'   <--- Throws Error 

import SOMETHING from '../SOMEJS.js' <--- No Error

But if you don't have the "default" in your export - like shown below, then you have to use the {} i.e. you should import it as a named export.


export class classA


import {a} from classA

 

  • Another reason could be missing the bracket {} in comment symbols .

<Tag>
/\*{ oldComponent }\*/   <--- Throws Error 
</Tag>


<Tag>
**{**/\*{ oldComponent }\*/**}**  <--- No Error
</Tag>

 

  • You might also be using a wrong version and you could change that by using the appropriate version in package.json. e.g.

"react-router-dom": "^x.y.z"

 

  • Use export default or require(path).default

    var X = require('./components/Home').default
    
 

  • You could also use import from "react-router-dom" which also sometimes fixes this issue. This is applicable for Higher or Latest React versions.

import { Link } from 'react-router' <--- Throws Error 

import SOMETHING from 'react-router-dom' <--- No Error

 

  • Try installing react-router-redux with npm. Better to use 5.x+. And change the package.json.

npm install react-router-redux@next


"react-router-redux": "<^NEW\_VERSION>"

  Hope this helps to solve the issue.    

Other Interesting Reads -

   


element type is invalid: expected a string but got: object ,element type is invalid: expected a string but got: undefined ,element type is invalid: expected a string jest ,element type is invalid: expected a string (for built-in components) or a class/function ,element type is invalid: expected a string react table ,element type is invalid: expected a string react-native ,uncaught invariant violation: spec.type must be defined ,did you accidentally export a jsx literal instead of a component? ,Invariant Violation: Element Type is Invalid ,invariant violation element type is invalid ,invariant violation element type is invalid react native ,invariant violation element type is invalid check the render method ,enzyme mount invariant violation element type is invalid ,invariant violation element type is invalid expected a string ,invariant violation element type is invalid expected a string but got object ,invariant violation element type is invalid expected a string (for built-in components) or ac ,element type is invalid in react native ,element type is invalid expected a string ,invariant violation element type is invalid expected a string (for built-in components) ,jest invariant violation element type is invalid expected a string ,uncaught invariant violation element type is invalid expected a string ,element type is invalid check the render method ,react invariant violation element type is invalid ,uncaught invariant violation element type is invalid ,element type is invalid expected a string react-native ,react native invariant violation element type is invalid expected a string ,invariant violation react native , , , ,enzyme mount invariant violation element type is invalid ,invariant violation element type is invalid ,invariant violation element type is invalid check the render method ,invariant violation element type is invalid dart ,invariant violation element type is invalid detected ,invariant violation element type is invalid django ,invariant violation element type is invalid expected a string ,invariant violation element type is invalid expected a string (for built-in components) ,invariant violation element type is invalid expected a string (for built-in components) or ac ,invariant violation element type is invalid expected a string but got object ,invariant violation element type is invalid hp ,invariant violation element type is invalid hpe ,invariant violation element type is invalid hplc ,invariant violation element type is invalid kafka ,invariant violation element type is invalid keras ,invariant violation element type is invalid kotlin ,invariant violation element type is invalid laravel ,invariant violation element type is invalid leetcode ,invariant violation element type is invalid length ,invariant violation element type is invalid literal ,invariant violation element type is invalid parameter ,invariant violation element type is invalid php ,invariant violation element type is invalid powershell ,invariant violation element type is invalid python ,invariant violation element type is invalid pytorch ,invariant violation element type is invalid query ,invariant violation element type is invalid queue ,invariant violation element type is invalid quora ,invariant violation element type is invalid quotes ,invariant violation element type is invalid react native ,invariant violation element type is invalid value ,invariant violation element type is invalid vba ,invariant violation element type is invalid vector ,invariant violation element type is invalid why ,invariant violation element type is invalid windows ,invariant violation element type is invalid xml ,invariant violation element type is invalid xor ,invariant violation element type is invalid xpath ,invariant violation element type is invalid yaml ,invariant violation element type is invalid yarn ,invariant violation element type is invalid yes or no ,invariant violation element type is invalid yet ,invariant violation element type is invalid yield ,invariant violation element type is invalid youtube ,invariant violation element type is invalid zendesk ,invariant violation element type is invalid zerodha ,invariant violation element type is invalid zip ,invariant violation element type is invalid zoho ,invariant violation element type is invalid zoom ,jest invariant violation element type is invalid expected a string ,react invariant violation element type is invalid ,react native invariant violation element type is invalid ,uncaught invariant violation element type is invalid ,uncaught invariant violation element type is invalid expected a string