The Problem
Module not found: Can't resolve '@/components/Button'
Common Causes and Solutions
1. Missing Path Aliases Configuration
// tsconfig.json or jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
2. File Extension Issues
// Wrong - missing extension in some cases
import Button from '@/components/Button'
// Check if file is Button.tsx, Button.ts, or Button/index.tsx
3. Case Sensitivity (Linux Servers)
// Wrong on Linux
import Button from '@/components/button'
// Correct - match exact case
import Button from '@/components/Button'
4. Clear Next.js Cache
rm -rf .next
rm -rf node_modules/.cache
npm run dev
5. Check next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
// Ensure proper module resolution
},
webpack: (config) => {
config.resolve.alias = {
...config.resolve.alias,
'@': path.resolve(__dirname, 'src'),
};
return config;
},
};
module.exports = nextConfig;
6. Reinstall Dependencies
rm -rf node_modules package-lock.json
npm install
Debugging Steps
- Verify file exists at the imported path
- Check for typos in import statement
- Verify tsconfig paths match your structure
- Clear all caches and restart
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!