next.js: TypeError: Cannot read property 'Component' of undefined
I am using the cloudinary-react library and have run into the following error message when I add the Image component to my project:
In the DOM
TypeError: Cannot read property 'Component' of undefined
at _callee3$ (http://localhost:3000/_next/-/main.js:3977:50)
at tryCatch (http://localhost:3000/_next/-/commons.js:36939:40)
at Generator.invoke [as _invoke] (http://localhost:3000/_next/-/commons.js:37230:22)
at Generator.prototype.(anonymous function) [as next] (http://localhost:3000/_next/-/commons.js:36991:21)
at step (http://localhost:3000/_next/-/commons.js:3724:30)
at http://localhost:3000/_next/-/commons.js:3742:14
at F (http://localhost:3000/_next/-/commons.js:2033:28)
at http://localhost:3000/_next/-/commons.js:3721:12
at doRender (http://localhost:3000/_next/-/main.js:3999:18)
at Object._callee$ (http://localhost:3000/_next/-/main.js:3891:20)
In the devtools
Warning: React attempted to reuse markup in a container but the checksum was invalid.
This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting.
React injected new markup to compensate which works but you have lost many of the benefits of server rendering.
Instead, figure out why the markup being generated is different on the client or server:
(client) tid="1"><div class="error" data-jsx="676
(server) tid="1"><div class="wrapper" data-jsx="2
The Image component takes in a cloudName & publicId. The publicId is a string that represents a photo on the Cloudinary API server attached to the user’s cloudName. Could it be that this is CORS issue and causing this error to appear? I’m not entirely sure what this is saying.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 22 (10 by maintainers)
I did have that code below:
import { React } from 'react';and I was getting that error: ‘Cannot read property ‘Component’ of undefined’I removed the curly braces, and it works fine
import React from 'react';And I reached in a conclusion: I need to learn ES6 🚶♂️Removing
babel-plugin-add-module-exportfixed it for me. Not sure why or how, but…Some observations:
Works fine with
next build && next startTypeError: Cannot read property 'Component' of undefinedstems fromclient/index.jsand these lines:where
component === window.__NEXT_DATA__.component.When loading the Page
doRender()is being called multiple times. In the first (and second?) executionComponenthas been declared as afunction(theevaled script[1]). That’s when you can observe the picture showing up. Now the next timedoRender()is calledComponentisundefinedand withlastAppPropsbeingundefined([2])—and therefore not having a propertyComponent([3])—results in theTypeError.Why
doRender()is being called multiple times andComponentisundefinedI haven’t figured out yet.The mismatch between server and client rendered code is a result of the server rendering
<img>element and the client thenexterror element.☕️
Same issue with Draft.js 🤔
@rockchalkwushock @czzarr See #1208
Removing babel-plugin-add-module-export from .babelrc also fixed it for me. But why?
@erikras Thanks. You saved my day. I met same problem. After removing
babel-plugin-module-resolverfrom .babelrc, the problem disappeared. Not sure why but it works.It looks something conflicting in babel plugin.
@timneutkens I am still running into this issue using next 2.1.1. Blew away my .next dir, cleared browser cache… still an issue