next.js: Next.js 13 - appDir - Error: Unsupported Server Component type: undefined
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro for Workstations
Binaries:
Node: 18.12.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.0.0
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
Chrome 106.0.5249.119 / Firefox Developer Edition 107.0b5
How are you deploying your application? (if relevant)
next start, Vercel (https://test-next-13-appdir.vercel.app/)
Describe the Bug
This issue only appears in dev mode
When I import a client component (the file has "use client" directive) from its exact location (e.g. ./src/components/Foo/Bar.tsx) and then use it in appDir, everything works fine, the component loads and there’s no issue.
But when I reexport it through ./src/components/Foo/index.ts with export * from "./Bar" and then import it in appDir like this: import { Bar } from "@/components", suddenly appDir stops working and then Next shows an issue like this: Error: Unsupported Server Component type: undefined (https://ibb.co/y01RPBv). I tried console.logging the component and what was logged is just undefined. The production build doesn’t have anything of the sort though, everything works just fine.
Thank you 😃
Expected Behavior
Everything should work just fine, the component should display as normal.
Link to reproduction
https://github.com/DuCanhGH/next-13-server-component-undefined
To Reproduce
- Run
pnpm i - Run
pnpm dev - Go to
http://localhost:3000and it should fail to load the page. I find it to be reproducible on both Chrome and Firefox.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 12
- Comments: 35 (17 by maintainers)
a workaround for me:
from
export * from './my-component'to
export { MyComponent } from './my-component'This is “solved” if you add the
"use client"directive to the top export (src/components/index.tsin this case).I agree this issue is quite annoying in a monorepo context.
Thanks for the tips everyone, for now I chose to explicitly reexport everything:
packages/some-workspace/src/index.tsI recommend it over the
client.tstrick because if the issue is fixed someday, there will be no need to update all the imports made within the apps:apps/some-app/src/some-component.ts@cwikio that’s why you need Typescript 💀
Ok, this bug put a nail in the coffin of my already fragile sanity after 10 years of js/node ecosystem.
So I started with this.
Ok, a seemingly understandable error, at least with some steps on how I should act upon it.
So adding
'use client'directive to it. (whoever thought of using this syntax is a fucking degenerate by the way)Fine, now, how the hell is this error:
Error: Unsupported Server Component type: undefinedwith a callstack direct from hell, likeIs SOMEWHAT helpful or indicative of the fact that to make this work, I just have to add the
use clientinstead of the component itself, to theindex.tswhere I re-export it.I’m fucking done with software development.
Will become a farmer. Peace!
The best way I found to work around this issue is exporting all components separately and creating a compound component and exporting that as well. Then use separate exports in the the RSC and the compound in RCC.
Example:
Same issue here with Next 13.1 and Typescript. I’ve found three workarounds for this:
default export MyComponentexport * from './MyComponent''use client'in that index file:Adding
"use client"to eachindex.tsis not the ideal solution, because there may be components that are not client components, which can cause new problems when I use non-client components, such as when I use a server component and pass a function into the componentImpressive, thanks @shuding ! @akomm let’s wait for the next canary build to see if it’s working I guess?
components/Foo/index.ts
components/Foo/foo.ts
This allows good “navigate to” and refactoring in IDEs and also better tree shaking as it purely uses ES style exports and not building “objects”. Imports like "import {Foo} from “~/app.src/components/foo” <- import autocomplete exists for the namespace. Then usage “Foo.Bar”.
Now If the Foo module has both client and RSC components, putting a “use client” at the top is a bad idea. But also working with “client.ts” makes things more complicated and cluttered than needed.
Sometimes, you also need the method mentioned by @feledori. For example when you have some root component and composition components to be used within it.
I’m seeing the same error when trying to import a client compound component in a server component, eg:
Where List is a client component that exposes another component on the
List.Itemproperty:Here’s a link to a reproduction: https://stackblitz.com/edit/vercel-next-js-jbt5nq?file=app/page.tsx
Thanks I will keep that in mind and proceed to do so
Thanks for the advice. I’m doing the same thing now
@awareness481 it really did, doesn’t seem to be neat, but it works for now 😄