| name | nextjs-import-enforcer |
| description | Automatically enforce consistent import patterns using Next.js path aliases and proper organization. Use when writing imports, creating new files, or organizing code structure. |
Import Convention Enforcer (Next.js/React)
Auto-enforces consistent import patterns using Next.js path aliases for clean, maintainable code.
Activation Triggers
This skill activates when:
- Writing or modifying TypeScript/JavaScript files
- Creating new components, hooks, utilities
- Importing from other files
- Organizing project structure
- Mentioning "import", "add", "create"
Required Import Pattern (MANDATORY)
Use path aliases for internal imports:
import { Button } from '@/components/ui'
import { useAuth } from '@/hooks'
import { formatDate } from '@/lib/utils'
import { User } from '@/types'
import { Button } from '../../components/ui/Button'
import { useAuth } from '../../../hooks/useAuth'
Path Alias Configuration
tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@/components/*": ["./src/components/*"],
"@/lib/*": ["./src/lib/*"],
"@/hooks/*": ["./src/hooks/*"],
"@/types/*": ["./src/types/*"],
"@/app/*": ["./src/app/*"]
}
}
}
Auto-Fix Process
Step 1: Detect Relative Imports
import { Button } from '../../../components/ui/Button'
import { Card } from '../../components/ui/Card'
import { useUser } from '../hooks/useUser'
Step 2: Convert to Path Aliases
import { Button } from '@/components/ui'
import { Card } from '@/components/ui'
import { useUser } from '@/hooks'
Step 3: Organize Imports
import { useState } from 'react'
import Link from 'next/link'
import { z } from 'zod'
import { useForm } from 'react-hook-form'
import { Button, Card } from '@/components/ui'
import { useUser } from '@/hooks'
import { formatDate } from '@/lib/utils'
import type { User } from '@/types'
Import Organization Rules
1. Import Order (Top to Bottom)
import { useState, useEffect } from 'react'
import Image from 'next/image'
import Link from 'next/link'
import { z } from 'zod'
import clsx from 'clsx'
import { Button } from '@/components/ui'
import { useAuth } from '@/hooks'
import { api } from '@/lib/api'
import type { User, Post } from '@/types'
import styles from './Component.module.css'
2. Named Imports (Alphabetical)
import { Button, Card, Dialog, Input } from '@/components/ui'
import { Dialog, Input, Button, Card } from '@/components/ui'
3. Type-Only Imports
import type { User } from '@/types'
import type { ButtonProps } from '@/components/ui'
import { type User, type Post } from '@/types'
Path Alias Patterns by Directory
Components
import { Button, Card, Input } from '@/components/ui'
import { UserProfile } from '@/components/features'
import { Header, Footer } from '@/components/layout'
Hooks
import { useAuth, useUser, useLocalStorage } from '@/hooks'
import { useAuth } from '@/hooks/useAuth'
Utilities/Lib
import { cn, formatDate, truncate } from '@/lib/utils'
import { api } from '@/lib/api'
import { ROUTES, API_URL } from '@/lib/constants'
Types
import type { User, Post, Comment } from '@/types'
import type { ApiResponse } from '@/types/api'
App Router (Next.js 13+)
import { generateMetadata } from '@/app/utils'
import { PageProps } from '@/app/types'
Barrel Exports (index.ts)
Encourage barrel exports for cleaner imports:
export { Button } from './Button'
export { Card } from './Card'
export { Input } from './Input'
export { Dialog } from './Dialog'
import { Button, Card, Input } from '@/components/ui'
Dynamic Imports
const Chart = dynamic(() => import('@/components/Chart'), {
loading: () => <Skeleton />,
ssr: false,
})
const Chart = dynamic(() => import('../../components/Chart'))
Server/Client Component Imports
import { db } from '@/lib/database'
import { ServerComponent } from '@/components/server'
'use client'
import { useState } from 'react'
import { Button } from '@/components/ui'
CSS/Style Imports
import { Button } from '@/components/ui'
import styles from './Component.module.css'
import '@/styles/globals.css'
Image Imports
import Image from 'next/image'
import logo from '@/public/logo.png'
<Image src="/logo.png" alt="Logo" width={200} height={50} />
Common Violations
Violation 1: Deep Relative Paths
import { Button } from '../../../components/ui/Button'
import { Button } from '@/components/ui'
Violation 2: Missing Barrel Export
import { Button } from '@/components/ui/Button'
import { Card } from '@/components/ui/Card'
import { Button, Card } from '@/components/ui'
Violation 3: Mixing Import Styles
import { Button } from '@/components/ui'
import { useAuth } from '../../hooks/useAuth'
import { Button } from '@/components/ui'
import { useAuth } from '@/hooks'
Violation 4: Type Import Without Keyword
import { User } from '@/types'
import type { User } from '@/types'
Project Structure Best Practices
src/
├── app/ # Next.js App Router
│ ├── (auth)/
│ ├── (dashboard)/
│ └── api/
├── components/
│ ├── ui/ # Shadcn components
│ │ ├── button.tsx
│ │ ├── card.tsx
│ │ └── index.ts # Barrel export
│ ├── features/ # Feature components
│ └── layout/ # Layout components
├── hooks/
│ ├── useAuth.ts
│ ├── useUser.ts
│ └── index.ts # Barrel export
├── lib/
│ ├── api.ts
│ ├── utils.ts
│ └── constants.ts
├── types/
│ ├── api.ts
│ ├── models.ts
│ └── index.ts # Barrel export
└── styles/
└── globals.css
ESLint Integration
Suggest ESLint rules:
{
"rules": {
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"pathGroups": [
{
"pattern": "@/**",
"group": "internal",
"position": "after"
}
],
"alphabetize": {
"order": "asc"
}
}
],
"import/no-relative-packages": "error"
}
}
Success Criteria
✅ ALL internal imports use path aliases
✅ NO deep relative paths (../..)
✅ Barrel exports for component directories
✅ Imports organized by category
✅ Type imports use type keyword
✅ Consistent import style across project
Behavior
Proactive enforcement:
- Detect relative imports automatically
- Convert to path aliases immediately
- Organize import order
- Suggest barrel exports
- Explain import patterns
Never:
- Require explicit "fix imports" request
- Allow deep relative paths
- Wait for linter errors
Always:
- Use
@/ path aliases
- Organize imports by category
- Add barrel exports where beneficial
- Add type keyword for type imports
This ensures clean, maintainable import structure from day one.