@tailwindcss/upgrade package provides automated codemods to migrate your Tailwind CSS project from v3 to v4. It handles CSS files, JavaScript configuration, and template files.
Installation
You don’t need to install the upgrade tool. Run it directly withnpx:
Basic usage
Navigate to your project root and run:- Verify your git directory is clean
- Check your Tailwind CSS version
- Discover and migrate CSS files
- Convert JavaScript configuration to CSS
- Update template files with new class names
- Update dependencies to v4
- Migrate PostCSS configuration if needed
Command-line options
Specify CSS files
By default, the tool discovers CSS files automatically. To specify specific files:Force migration
Bypass the git clean check (not recommended):Custom config path
Specify a custom Tailwind configuration file:Show help
Display usage information:Show version
Display the tool version:What gets migrated
CSS file migrations
The tool performs these CSS transformations:Tailwind directives
Before:Custom imports
Before:Layer utilities
Before:Apply statements
Before:@apply are updated to v4 syntax.
Media screen queries
Before:JavaScript config migrations
The tool converts yourtailwind.config.js to CSS:
Theme customization
Before:Container configuration
Before:Dark mode
Before:Custom variants
Before:Data/aria variants
Before:Template file migrations
The tool updates class names in all your template files:Class name updates
Before:Arbitrary values to named values
The tool converts arbitrary values to named values where possible: Before:Variant order
Class variants are canonicalized to the correct order: Before:Group variants
Some group/peer patterns are updated: Before:Modifier shorthand
Modifiers are converted to the new syntax: Before:Dependency updates
The tool updates these packages to their latest versions:package.json are updated.
PostCSS config migration
For PostCSS-based projects: Before:File discovery
The upgrade tool automatically discovers files to migrate:CSS files
Searches for**/*.css files while:
- Respecting
.gitignorerules - Skipping
node_modules - Only processing files containing Tailwind directives
Template files
Scans files based on your@source directives or discovers automatically:
- JavaScript:
.js,.jsx,.ts,.tsx - Vue:
.vue - Svelte:
.svelte - HTML:
.html,.htm - PHP:
.php - Ruby:
.erb,.haml,.slim - And more…
Ignored files
These are automatically skipped:node_modules/.git/.next/.turbo/.vercel/.pnpm-store/- Binary files (
.png,.jpg,.wasm,.node, etc.) - Log files (
.log) - Database files (
.db,.sqlite)
Migration strategies
The tool uses several strategies to ensure safe migrations:Safe migration checks
Before migrating a class, the tool verifies:- Not in a code comment - Skips migrations in comments
- Not in a string literal - Avoids changing unrelated strings
- Not in event handlers - Preserves JavaScript like
addEventListener('blur', ...) - Valid in context - Ensures the migration makes sense
Canonicalization
Classes are canonicalized to their preferred form:- Variants in the correct order
- Arbitrary values simplified where possible
- CSS variable syntax updated
- Modifiers in standard format
Caching
The tool caches migration results for performance:- Each unique class is processed once
- Results are reused across all files
- Significantly speeds up large projects
Handling edge cases
Multiple config files
For monorepos or projects with multiple configs:- Multiple CSS entry points
- Shared configuration files
- Workspace dependencies
Dynamic class generation
Some dynamic classes can’t be migrated automatically:- Review the migration output
- Manually update dynamic class generation
- Use a linter to catch issues
Template syntax
The tool understands various template syntaxes: Vue:Preserving formatting
The tool attempts to preserve:- Indentation
- Line breaks
- Code style
- Comments
CSS files are formatted using Prettier after migration for consistency.
After migration
Review changes
Always review the changes made:- Unexpected class changes
- Configuration that wasn’t migrated
- Template files that need manual updates
Test your build
Run your build to catch any issues:- Missing dependencies
- Plugin compatibility
- Custom configuration that needs manual migration
Visual testing
Perform visual regression testing:- Check that UI looks correct
- Verify dark mode still works
- Test responsive breakpoints
- Validate animations and transitions
Commit changes
Once verified, commit the migration:Troubleshooting
Tool crashes
If the upgrade tool crashes:Version mismatch
Config not migrating
Some configs require manual migration:- Complex plugin functions
- Dynamic theme values
- Custom match variants with complex logic
Classes not updating
If classes aren’t migrated:- Check file extension - Ensure the file type is recognized
- Check .gitignore - File might be ignored
- Check syntax - Dynamic classes won’t migrate
- Run manually - Specify the file explicitly
Build errors after migration
If you get build errors:-
Clear build cache:
-
Reinstall dependencies:
-
Check for plugin updates:
- Ensure third-party plugins support v4
- Update or remove incompatible plugins
Advanced usage
Incremental migration
For large projects, migrate incrementally:Custom migrations
For custom migrations beyond what the tool provides:- Use the tool for standard migrations
- Write custom scripts for project-specific patterns
- Use find/replace for bulk updates
- Leverage editor regex capabilities
Monorepo migration
For monorepos:Limitations
What’s NOT migrated automatically
- Complex JavaScript plugins - Custom plugin logic
- Dynamic class generation - Runtime-generated classes
- Some theme functions - Complex theme() usage
- Custom CSS-in-JS - Styled-components, Emotion, etc.
- Build scripts - Custom build configurations
- Third-party plugins - May need updates from maintainers
Framework-specific considerations
Next.js:- App Router vs Pages Router differences
- Server Components considerations
- Image optimization integration
- Route-based styles
- PostCSS integration
- Scoped styles in components
- Vite plugin integration
Migration checklist
Use this to track your migration:- Backup project or commit all changes
- Run
npx @tailwindcss/upgrade - Review
git difffor all changes - Test build process
- Visual regression testing
- Update custom plugins if needed
- Check dark mode implementation
- Verify responsive design
- Test in all target browsers
- Update documentation
- Commit the migration
Getting help
If you need assistance:- Read the Upgrade Guide
- Check v3 to v4 Migration Guide
- Visit GitHub Discussions
- Report bugs at GitHub Issues
- Review the CHANGELOG