1.0.3 β’ Published 5 months ago
next-navigation-migrator v1.0.3
Next.js Navigation Migrator
A powerful codemod library to migrate your Next.js application from the Pages Router (next/router
) to the App Router (next/navigation
). This tool intelligently transforms your components to use the new router APIs with minimal manual intervention.
Features
- π Automatically replaces
next/router
imports with appropriatenext/navigation
hooks - π§© Transforms router properties like
query
andpathname
to their Navigation API equivalents - π Handles different query parameter patterns including array parameters
- π§Ή Removes unnecessary router declarations when only query/pathname are used
- π Adds useful code comments for manual migration steps
- π Automatically adds the
'use client'
directive where needed - π Works with both JavaScript and TypeScript codebases
Installation
Global Installation (Recommended)
npm install -g next-navigation-migrator
Local Installation
npm install --save-dev next-navigation-migrator
Usage
Command Line Interface
Run the migration on your entire Next.js project:
# If installed globally
next-navigation-migrator ./path/to/your/project
# If installed locally
npx next-navigation-migrator ./path/to/your/project
What This Codemod Does
Based on the official Next.js migration guide and common patterns, this codemod handles the following key migration points:
Import Path Changes
- Changes imports from
next/router
tonext/navigation
- Adds appropriate imports for additional hooks when needed (
useSearchParams
,usePathname
) - Handles both named and default imports (or an empty import thatβs missing required hooks)
Router Property Transformations
router.pathname
βusePathname()
router.query
βuseSearchParams()
router.query.param
βuseSearchParams().get('param')
router.query.arrayParam
βArray.from(useSearchParams().getAll('arrayParam'))
router.asPath
βusePathname()
(with comments on query differences)- Destructuring like
const { query } = router
βconst query = useSearchParams()
- Object destructuring like
const { id } = router.query
βconst id = useSearchParams().get('id')
Optimization
- Removes unnecessary
const router = useRouter()
declarations when only used for query/pathname - Updates
useEffect
dependencies fromquery.param
tosearchParams
Router Methods
- Updates
router.push()
androuter.replace()
to remove unsupported options (such asshallow
) and extra arguments, retaining only supported options such asscroll
- Adds comments for manual review of router method transformations
License
This project is licensed under the MIT License.