Next.js Discord

Discord Forum

"You have a Server Component that imports next/router. Use next/navigation instead" in pages folder

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I'm been developing with the pages router and tried creating a new folder "app" folder under my src directory and adding an endpoint (I want to slowly migrate), but now it seems to think that components in my pages directory are all meant to be server-components, so it's giving me an error. Do I have to add 'use client' to everything, or does someone know why this is happening?

├── src
│  ├── app (new)
│  │   └── api
│  │   └── trpc
│  │   └── [trpc]
│  │   └── route.ts
│   ├── components
│   ├── env.mjs
│   ├── fixtures
│   ├── pages
│   ├── server
│   ├── styles
│   ├── trpc
│   └── utils
Answered by American black bear
I just tried installing and upgrading my project packages again and it worked somehow, thanks for answering!
View full answer

16 Replies

American black bearOP
@Masai Lion okay, It is quite a bit of files that I have to refactor with 'use client' at the top in my production repo. I tried to create a new project using "create-next-app@latest" with both a "pages" and an "app" folder, and then I did not need 'use client' at the top of "pages" files, it builds just fine.
im just saying that the components in the /pages folder are all already converted to "use client". They are all client components

in /app folder, the default in server component. Your step one would be to create a "use client" file and put everything from /pages in that file for now
I did not need 'use client' at the top of "pages" files, it builds just fine.
I didnt tell you to add "use client" at the top of "pages" files
American black bearOP
When i build the project, it says I still need 'use client' at the top of files under /pages, that's the problem.
you use "use client" to create server-to-client boundaries in the app dir.
But not in pages dir.

So if you want to start mgirating, then you can just add it for now in your components
American black bearOP
I just thought this error shouldn't appear unless it's under the /app folder
@American black bear I just thought this error shouldn't appear unless it's under the /app folder
yeah in app router they use a different router
its import { useRtouer } from 'next/navigation' and the methods are a bit different
so you have to extract that logic for now
American black bearOP
yeah I thought so too, but I thought that I could still use the old router in my pages directory (the error above is from a file under /src/pages/...)
but i hope i answered half of your question, yeah to migrate from pages to app dir, you may need to write 'use client' everywhere at first.
American black bearOP
I just tried installing and upgrading my project packages again and it worked somehow, thanks for answering!
Answer