Module aliases don't work with multiple app routers
Answered
Lonely Cat posted this in #help-forum
Problem: Nextjs doesnt want to let me import modules/functions from inside app router. I really hope theres a dumb fix to this im overlooking, major thanks ❤️
Error:
src/app/(authentication)/auth/login/page.tsx:
src/app/(authentication)/_components/login.tsx:
tsconfig.json:
Error:
- ./src/app/(authentication)/auth/login/page.tsx
- Module not found: Can't resolve '@/app/(authentication)/_components/login'src/app/(authentication)/auth/login/page.tsx:
import { LoginForm } from "@/app/(authentication)/_components/login";
const LoginPage = () => {
return (
<LoginForm />
);
}
export default LoginPage;src/app/(authentication)/_components/login.tsx:
export const LoginForm = () = > {...}tsconfig.json:
{
"compilerOptions": {
"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", "next-auth.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}Answered by Lonely Cat
No worries bro, thanks for replying, but the issue was with my nextjs version. I bumped to 14.2.6 and everything worked lol.
5 Replies
@Lonely Cat **Problem:** Nextjs doesnt want to let me import modules/functions from inside app router. I really hope theres a dumb fix to this im overlooking, major thanks ❤️
**Error:**
diff
- ./src/app/(authentication)/auth/login/page.tsx
- Module not found: Can't resolve '@/app/(authentication)/_components/login'
**src/app/(authentication)/auth/login/page.tsx:**
tsx
import { LoginForm } from "@/app/(authentication)/_components/login";
const LoginPage = () => {
return (
<LoginForm />
);
}
export default LoginPage;
**src/app/(authentication)/_components/login.tsx:**
tsx
export const LoginForm = () = > {...}
**tsconfig.json:**
json
{
"compilerOptions": {
"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", "next-auth.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
huh that's odd, since the
page.tsx file shares the same src/app/(authentication), might as well use ../../components/login in the meantime, and see if that workedi compared my regular
tsconfig.json file with yours and the one that differed is the moduleResolution (you have it set as "bundler"), whilst I have it set as "node". could you try changing that to "node" to see whether it solves the problem?i don't have much experience about this
moduleResolution field, maybe if someone could drop-in a quick explanation about it would be pretty helpfulthis doc seemed to explain this quite well https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution
@iyxan23 huh that's odd, since the `page.tsx` file shares the same `src/app/(authentication)`, might as well use `../../components/login` in the meantime, and see if that worked
No worries bro, thanks for replying, but the issue was with my nextjs version. I bumped to 14.2.6 and everything worked lol.
Answer