Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: fails to compile on nextjs Client Component SSR - Error: Element type is invalid: expected a string #5823

Closed
2wheeh opened this issue Apr 4, 2024 · 3 comments · Fixed by #5833

Comments

@2wheeh
Copy link
Contributor

2wheeh commented Apr 4, 2024

Lexical version: 0.14.3

Steps To Reproduce

  1. clone https://github.com/2wheeh/nextjs-lexical-build-error
  2. try build (or compile dynamic route pages by accessing localhost:3000/not-work/hi)

Link to code example: https://github.com/2wheeh/nextjs-lexical-build-error

The current behavior

Call apis or anything imported from Lexical in ClientComponent (with 'use client' directive).
Rendering this component in nextjs server side fails.

// client-component.tsx
'use client';

import { COMMAND_PRIORITY_CRITICAL } from 'lexical'; // just a simple constant. 

export default function ClientComponent() {
  COMMAND_PRIORITY_CRITICAL;

  return <>hi</>;
}
// not-work/page.tsx
import ClientComponent from '../client-component';

export default function Page() {
  return <ClientComponent />;
}
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

build time: fails to generate static pages.

image

run time: fails to compile dynamic ssr pages.

image

Now It only works with dynamic imports with no ssr.

// work/page.tsx
import dynamic from 'next/dynamic';

const ClientComponent = dynamic(() => import('../client-component'), { ssr: false });

export default function Page() {
  return <ClientComponent />;
}

The expected behavior

works well as it was til 0.14.2

@2wheeh 2wheeh changed the title Bug: fails to build on nextjs generating static pages - Error: Element type is invalid: expected a string Bug: fails to compile on SSR - Error: Element type is invalid: expected a string Apr 5, 2024
@2wheeh 2wheeh changed the title Bug: fails to compile on SSR - Error: Element type is invalid: expected a string Bug: fails to compile on nextjs SSR - Error: Element type is invalid: expected a string Apr 5, 2024
@2wheeh 2wheeh changed the title Bug: fails to compile on nextjs SSR - Error: Element type is invalid: expected a string Bug: fails to compile on nextjs Client Component SSR - Error: Element type is invalid: expected a string Apr 5, 2024
@etrepum
Copy link
Contributor

etrepum commented Apr 5, 2024

Looking into it, so far I have no idea what's going on with webpack/swc here. All of the errors are very ugly and happen after a lot of compilation has already taken place.

@etrepum
Copy link
Contributor

etrepum commented Apr 5, 2024

For reference, here's what the stack trace looks like in the project I've reproduced this in
⨯ Internal error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:43579)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:43309)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at aC (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:50458)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:38912
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:39398)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at aC (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:50458)
    at ak (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:49564)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46881)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:41912)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at as (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7390)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10825)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at as (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7390)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10825)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:39541)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:39541)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at as (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7390)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:10825)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at av (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:7809)
    at /Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:12972
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:13009)
    at aS (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:46552)
    at ab (/Users/bob/src/lexical-esm-nextjs/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:35:39541)

@2wheeh

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants