Can't export a component that needs next/headers in the same file as a normal component
Unanswered
Somali posted this in #help-forum
SomaliOP
I like to barrel files together. When I try to import and export a file that uses next/headers and another normal componenet in the same file I get "you're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory." I'm not even using page router though.
Funny enough it works if they are imported separately.
Funny enough it works if they are imported separately.
10 Replies
SomaliOP
Error:
File structure:
Inside index.tsx:
export { getTheme } from "./theme.context";
export { ThemeProviderWithCookies as ThemeProvider } from "./cookieTheme";
export type { ThemeStateProps } from "./theme.context";The error only happens when I import ThemeProvider from the index.tsx above then try to use getTheme
But if instead import these from seperate files it starts to work
SomaliOP
This might be a bug since it works normally
@Somali I like to barrel files together. When I try to import and export a file that uses next/headers and another normal componenet in the same file I get "you're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory." I'm not even using page router though.
Funny enough it works if they are imported separately.
barrel files doesn't play well in next 14.
if a index.tsx exporting client components and server component, it will mark all component to be client
if a index.tsx exporting client components and server component, it will mark all component to be client