/pages not recongnised as root dir for routing
Answered
Swedish Lapphund posted this in #help-forum
Swedish LapphundOP
So I currently have the following setup:
64 Replies
Swedish LapphundOP
"use client"
import { useRouter } from 'next/navigation';
const HomePage = () => {
const router = useRouter();
const navigateToCompany = () => {
router.push('/company/page');
};
const navigateToPanel = () => {
router.push('/panel/page');
};
const navigateToAdmin = () => {
router.push('/admin/page');
};
return (
<div>
<h1>Main Page</h1>
<button onClick={navigateToCompany}>Go to Company Page</button>
<button onClick={navigateToPanel}>Go to Panel Page</button>
<button onClick={navigateToAdmin}>Go to Admin Page</button>
</div>
);
};
export default HomePage;this is the
/pages/page.tsxI basically want a landing page, and the option for people to navigate to a specific panel. You have 3 variations:
Panel (member), Company and Admin panel.current if I would want to visit the page shown above, I have to do:
http://localhost:3000/pages, however I would wanat it to be http://localhost:3000/Giant panda
Your
pages folder is inside appAnswer
Giant panda
That doesn't make sense
So remove the pages dir
And pull your pages into
app directlySwedish LapphundOP
what does the
app represent then, since this all would full under the same applet me try what you suggested right now
Giant panda
You don't have to place everything in app
For example your components folder that probably has reusable components can be in
srcWhich also helps to prevent mistakes from accidentally making files routable
Swedish LapphundOP
should I even have a
/app dir to begin with?Giant panda
If you want to use the app router you HAVE to
@Giant panda And pull your pages into `app` directly
Swedish LapphundOP
meaning I omit the pages dir, and have them just in app?
okay, so I have this now:
that does the trick, now however I am wondering how to handle routing for each sub dir
Giant panda
Offtopic: don't use buttons for links. Just use
<Link /> itself.Swedish LapphundOP
Thanks, Ill adjust that rigth away
@Swedish Lapphund that does the trick, now however I am wondering how to handle routing for each sub dir
Giant panda
Then you have to read the docs to understand how the app router works.
Swedish LapphundOP
it worked previously, I am not sure why it doesnt anymore...
Giant panda
Delete the
.next folder and try againSwedish LapphundOP
> Couldn't find a `pages` directory. Please create one under the project root
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
PS D:\Documents\coding\Accessibility-Panel-Nextjs> same thing
Giant panda
Which version of Next are you using?
Swedish LapphundOP
"next": "14.0.3",Giant panda
Technically it should work. Try restarting VSCode entirely.
Swedish LapphundOP
Same result, should I try to reinstall my dependencies?
Giant panda
That shouldn't be necessary.
Show me your tsconfig and next.config files please.
Swedish LapphundOP
tsconfig.json{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}next.config.js/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = nextConfigGiant panda
Nothing wrong there
Only suggestion I have is to delete
.next, node_modules and fully restart your PC and give it another try. Windows could be doing something really stupid.Swedish LapphundOP
I restarted, and rmoved those folders prior to that
now I will try yarn install
that cannot be normal
look at the amount
of dependencies
Giant panda
Welcome to JS
That is completely normal
Swedish LapphundOP
same issue though
Giant panda
Maybe it's a regression. Try using 14.0.4 of Next
Editing it in the package.json and using
yarn install afterwardsIf that doesn't work either try scaffolding a new app with
create-next-app and in the worst case scenario copy your files overSwedish LapphundOP
that worked
@Giant panda Maybe it's a regression. Try using 14.0.4 of Next
Swedish LapphundOP
this
why though?
Giant panda
It can always be that a version has bugs
14.0.3 may just have a bug that the app router isn't correctly recognized 🤷â€â™‚ï¸
Swedish LapphundOP
fair enough, tricky bug that is though lol
it worked perfectly fine
I didnt change anything, just run dev after stopping it, and boom.
Giant panda
Welcome to Software Engineering
Swedish LapphundOP
I current have this right, the
page.tsx works just fine, like this:now, when I try:
panel/dashboard it gives a 404, same with /dashboard.Giant panda
Because those aren't page files.
Check the docs for details on routing and if you have questions create a new thread.
Swedish LapphundOP
!solve