Help with Pages routing
Answered
Chinese Alligator posted this in #help-forum
Chinese AlligatorOP
I'm learning Nextjs and having trouble configuring Nextjs to use Pages routing instead of App routing. I've created the Pages folder and moved all the files to that folder but all the files show "untracked". I've modified next.config.mjs but it looks like the routing is using App routing. I feel like this is very simple but since I'm new at Nextjs. I'm quite lost with this. Can anyone help? Thank you.
15 Replies
Laysan Albatross
did you update next.js to the latest version? app router is available for V13 and higher.
Chinese AlligatorOP
I don't want to use App router. The current version is "next": "14.1.0" so do I need to change it to back to older version? But I do see that the newer version still supports the Pages Router.
Laysan Albatross
ah lol why would you wanna do that?
The support for the pages directory is just for legacy projects, but best practice is the app router of course
Chinese AlligatorOP
I'm really new at this so I figured using the pages routing is much simpler for learning curve sake.
Laysan Albatross
hmmm, the thing is, you'll face the problem that all new learning content is made for /app & also the people on this discord are assuming you're using /app .
But if you have good reasons, its definitely possible to use the pages directory. Actually When you init a new project you're asked if you wanna use the pages directory, so I would just do the same thing to your project that the init script is doing for new projects.
But if you have good reasons, its definitely possible to use the pages directory. Actually When you init a new project you're asked if you wanna use the pages directory, so I would just do the same thing to your project that the init script is doing for new projects.
Chinese AlligatorOP
I chose "no" for the question regards to if want to use "App Router" but the src/app still showed up but no pages folder. I manually created pages folder and insert an index.js but localhost can't find it. So it looks like the setup is still using App router.
Chinese AlligatorOP
Anyway, I lowered the next version and it seemed to work fine. So for the purpose of learning I'll just use the older version for now.
Silver
You can still use the pages routing with NextJS 14, support hasn't been removed - so you shouldn't have to rollback versions to fix the issue. One thing to note is if you have a file in both the app router and the pages router of the same name then it will throw an error since there is a collision (two files pointing to the same route). I would agree with fschlegelone though, the app router is the recommended approach and a better thing to ramp up on. Pages router has been around for a long time so if you work in nextjs projects you are probably going to have to support it for a long time too, so no harm in understanding both.
Chinese AlligatorOP
I changed my configuration but no Pages routing was available and only App Routing under the NextJS 14. Perhaps, I'm missing something?
Laysan Albatross
First I need you to never use npm or yarn ever again and go for pnpm. Basically it's a better npm, and every package available for npm is also available for pnpm.
Because the last thing you want in your project is mixing different package managers.
Because the last thing you want in your project is mixing different package managers.
Than you select exactly these options when creating a new next app
Laysan Albatross
And here you go 🙂
Answer
Laysan Albatross
As you can see, it works for the latest Next.js version, without any problems.
But keep in mind what I told you.
Best practice is to use the app router and I'm pretty sure you will learn page routing with the /app directory as fast as with the /pages directory.
So if you have good reasons to use the pages directory, go for it, I showed you how you can do it.
But if you are very unsure about it, go for the app directory, because it's more valuable to learn the latest best practices than learning an old way of doing things that will not be supported forever.
Best practice is to use the app router and I'm pretty sure you will learn page routing with the /app directory as fast as with the /pages directory.
So if you have good reasons to use the pages directory, go for it, I showed you how you can do it.
But if you are very unsure about it, go for the app directory, because it's more valuable to learn the latest best practices than learning an old way of doing things that will not be supported forever.