How to fix "Module not found" error when using path aliases in Next.js? #87098
-
SummaryI'm using Next.js 14 and configured path aliases in tsconfig.json: { But I keep getting "Module not found: Can't resolve '@/components/Button'" error. What am I missing? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi! The issue is that Next.js needs the same alias configuration in 1. Update const path = require('path'); module.exports = { 2. Or simpler - just restart the dev server: Sometimes the alias works after restarting:
3. Make sure your folder structure matches: your-project/ Hope this helps! 🚀 |
Beta Was this translation helpful? Give feedback.
Hi!
The issue is that Next.js needs the same alias configuration in
next.config.js. Here's the fix:1. Update
next.config.js:const path = require('path');
module.exports = {
webpack: (config) => {
config.resolve.alias['@'] = path.resolve(__dirname, 'src');
return config;
},
};
2. Or simpler - just restart the dev server:
Sometimes the alias works after restarting:
npm run devagain3. Make sure your folder structure matches:
your-project/
├── src/
│ └── components/
│ └── Button.tsx
├── tsconfig.json
└── next.config.js
Hope this helps! 🚀