next-page-to-app-converter
v1.1.0
Published
Convert Next.js Pages Router to App Router
Maintainers
Readme
Next.js Pages to App Router Converter
Automate your migration from Next.js Pages Router to App Router with intelligent component analysis and code transformation.
⭐ If this project saves you time, consider starring the repository and supporting its continued development.
🚀 Key Features
- Smart Component Analysis: Automatically determines if a component should be Server or Client based on hooks and browser API usage.
- Data Fetching Migration: Converts
getServerSidePropsandgetStaticPropsto async Server Components, includinggenerateStaticParams. - Routing Updates: Transforms
next/routertonext/navigation(useRouter,usePathname,useSearchParams). - API Routes: Migrates API handlers to Route Handlers, updating
resmethods toNextResponse. - Metadata: Converts
<Head>tags to the Metadata API. - Layouts & Providers: Automatically generates root layouts, nested layouts, and extracts context providers from
_app.tsx. - Font Optimization: Migrates
next/fontconfigurations.
📦 Installation & Usage
Quick Run
Run directly using npx:
npx next-page-to-app-converter <project-path>Global Installation
npm install -g next-page-to-app-converter
next-pages-to-app <project-path>⚙️ Options
| Flag | Description |
|------|-------------|
| -d, --dry-run | Preview the migration without writing any files. |
| -v, --validate | (Experimental) Runs runtime validation using Next.js DevTools (Requires Next.js 16+). |
| -h, --help | Show all available commands and options. |
🛠️ How It Works
Scans your
pagesdirectory for:- Routes
- API endpoints
- Special files (
_app,_document,_error)
Analyzes every component and its imports to detect:
- React Hooks (
useState,useEffect, etc.) - Event handlers (
onClick,onChange, etc.) - Browser-only APIs (
window,document,localStorage, etc.)
- React Hooks (
Transforms your code by:
- Adding
"use client"directives where required. - Rewriting data fetching to async Server Components.
- Updating navigation and routing logic.
- Converting metadata handling.
- Migrating API routes.
- Adding
Generates a complete
appdirectory while preserving your application's behavior as closely as possible.
✅ Requirements
- Node.js: 18.0.0 or later
- Target Project: Next.js 13 or later
- Validation: Next.js 16+ required for the
--validateoption
❓ Troubleshooting
Validation Issues
The --validate flag starts your development server for runtime validation.
Before using it, make sure the following command runs successfully:
npm run devComplex Projects
While the converter handles most common migration patterns, projects using:
- Complex custom server logic
- Dynamic runtime imports
- Advanced webpack customization
- Unusual routing patterns
may require manual review after migration.
❤️ Support
If this tool saves you hours of migration work, consider supporting its development.
Your support helps fund:
- 🚀 New migration features
- 🐞 Bug fixes
- ⚡ Performance improvements
- 📚 Better documentation
- ❤️ Continued maintenance
Every contribution is greatly appreciated.
📄 License
ISC
🙏 Acknowledgments
- Built with ts-morph for TypeScript AST manipulation.
- Inspired by the Next.js community's migration needs.
- Thanks to everyone who reports bugs, contributes code, and supports the project.
