I am running the tutorial for Next.js, and the first steps are very simple. Create a new page, and navigate to it after starting up your dev server on localhost:3000.
The new page is in the src/app
directory, which according to the documentation has recently replaced the pages
directory, so as far as I can tell, there should be no problem. but when I navigate to http://localhost:3000/posts/first-post I get a 404.
The following is my folder structure:
Contents of my jsconfig.json
file:
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
I tried changing the directory of course, moving it in and out the src/app directory, I tried re-starting the dev server, and I have tried changing the contents of the jsconfig.json
file from
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
To
{
"compilerOptions": {
"paths": {
"@/*": ["./src/app/*"]
}
}
}
3
Answers
When migrating from
pages
toapp
structure, the naming of the file matters.In
pages
if you named your file a certain way, it created a route with the name of the file (in your case first-post). With theapp
folder structure, your pages have to been namedpage.[txs|jsx|js]
to work.So if you wanted a page at
/posts/first-post
you would need a structure likeYou can always refer to the documentation for more details.
That’s because Next is looking for a page.js file and isn’t finding any, therefore not making not even the route ‘posts’ public.
You need to create a new folder "first-post" and inside a page.js file.
Check the documentation here.
That’s simply because, as per Nextjs Documentation :
Each segment requires a page.[tsx,jsx] file. In this case, It is necessary for you to rearrange your files in the following format: