Need help with setting up Module Path Aliases
Answered
Brown bear posted this in #help-forum
Brown bearOP
Okay, hitting a wall here, and I can't figure out what's wrong. I'm trying to set up module path aliases, but something is of. I followed what's described in the docs here: https://nextjs.org/docs/app/getting-started/installation#set-up-absolute-imports-and-module-path-aliases
This is my
But when I import a component like this for example:
I get a build error:
The relative path would be:
The odd thing is, that VS Code can resolve the path. It suggests me imports like this, and I can even Ctrl-click on the
This is my
jsconfig.json:
{
"compilerOptions": {
"baseUrl": "src/",
"paths": {
"@/components/*": ["components/*"]
}
}
}But when I import a component like this for example:
import { Timeline } from "@/components/Timeline";I get a build error:
Module not found: Can't resolve '@/components/Timeline'The relative path would be:
src/components/Timeline.jsThe odd thing is, that VS Code can resolve the path. It suggests me imports like this, and I can even Ctrl-click on the
Timeline import, and it jumps to the right file.Answered by Poodle
There it is. When you have a tsconfig.json it ignores jsconfig.json entirely. Lets try adding baseUrl and paths to your tsconfig compilerOptions instead:
Delete jsconfig.json after that and restart the server.
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}Delete jsconfig.json after that and restart the server.
7 Replies
@Brown bear Okay, hitting a wall here, and I can't figure out what's wrong. I'm trying to set up module path aliases, but something is of. I followed what's described in the docs here: https://nextjs.org/docs/app/getting-started/installation#set-up-absolute-imports-and-module-path-aliases
This is my `jsconfig.json`:
json
{
"compilerOptions": {
"baseUrl": "src/",
"paths": {
"@/components/*": ["components/*"]
}
}
}
But when I import a component like this for example:
`import { Timeline } from "@/components/Timeline";`
I get a build error:
`Module not found: Can't resolve '@/components/Timeline'`
The relative path would be:
`src/components/Timeline.js`
The odd thing is, that VS Code can resolve the path. It suggests me imports like this, and I can even Ctrl-click on the `Timeline` import, and it jumps to the right file.
Poodle
have you tried killing the dev server and restarting? Next.js caches jsconfig on startup so changes dont always pick up. If that doesnt fix it, try this setup instead:
also double check you dont have a tsconfig.json thats overriding it.
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}also double check you dont have a tsconfig.json thats overriding it.
@Poodle have you tried killing the dev server and restarting? Next.js caches jsconfig on startup so changes dont always pick up. If that doesnt fix it, try this setup instead:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
also double check you dont have a tsconfig.json thats overriding it.
Brown bearOP
Yep, always restarted after any changes to the jsconfig. Even tried to delete the .next folder once. Sadly, your config snippet didn't work either.
My tsconfig looks like this:
My tsconfig looks like this:
{
"compilerOptions": {
"target": "ES2017",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}@Brown bear Yep, always restarted after any changes to the jsconfig. Even tried to delete the .next folder once. Sadly, your config snippet didn't work either.
My tsconfig looks like this:
json
{
"compilerOptions": {
"target": "ES2017",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
Poodle
There it is. When you have a tsconfig.json it ignores jsconfig.json entirely. Lets try adding baseUrl and paths to your tsconfig compilerOptions instead:
Delete jsconfig.json after that and restart the server.
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}Delete jsconfig.json after that and restart the server.
Answer
@Poodle There it is. When you have a tsconfig.json it ignores jsconfig.json entirely. Lets try adding baseUrl and paths to your tsconfig compilerOptions instead:
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
Delete jsconfig.json after that and restart the server.
Brown bearOP
I really wished this would have been the solution, but nope. Still no luck:
while in the
Btw, when I do it as relative import like
## Error Type
Build Error
## Error Message
Module not found: Can't resolve '@/components/Timeline'
## Build Output
./src/app/about/page.js:3:1
Module not found: Can't resolve '@/components/Timeline'
1 | import Link from "next/link";
2 |
> 3 | import { Timeline } from "@/components/Timeline";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |
5 | export const metadata = {
6 | title: {
https://nextjs.org/docs/messages/module-not-found
Next.js version: 16.1.6 (Turbopack)while in the
tsconfig.json I have:
"exclude": [
"node_modules"
],
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}Btw, when I do it as relative import like
import { Timeline } from "../../components/Timeline"; it works perfectly fine@Brown bear I really wished this would have been the solution, but nope. Still no luck:
## Error Type
Build Error
## Error Message
Module not found: Can't resolve '@/components/Timeline'
## Build Output
./src/app/about/page.js:3:1
Module not found: Can't resolve '@/components/Timeline'
1 | import Link from "next/link";
2 |
> 3 | import { Timeline } from "@/components/Timeline";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 |
5 | export const metadata = {
6 | title: {
https://nextjs.org/docs/messages/module-not-found
Next.js version: 16.1.6 (Turbopack)
while in the `tsconfig.json` I have:
json
"exclude": [
"node_modules"
],
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
Btw, when I do it as relative import like `import { Timeline } from "../../components/Timeline";
` it works perfectly fine
Poodle
Ah you put baseUrl and paths at the root level but they need to be inside compilerOptions. Move them inside and it should work:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/": ["./src/"]
}
},
"exclude": ["node_modules"]
}@Poodle Ah you put baseUrl and paths at the root level but they need to be inside compilerOptions. Move them inside and it should work:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/": ["./src/"]
}
},
"exclude": ["node_modules"]
}
Brown bearOP
Whoops, my bad. Didnt pay enough attention. Thank you very much, now it works
Thank you very much :)
Thank you very much :)