Step-by-step guide to upgrade v4 to Strapi v5
Hanzala
9 min read ⋅ Sept 29, 2024
Strapi, the popular open-source headless CMS, has released its version 5 with significant improvements and new features. This guide will walk you through the process of upgrading your Strapi application from version 4 to version 5, ensuring a smooth transition for your projects.
Table of Contents
- Introduction
- Prerequisites
- Step-by-Step Upgrade Process
- Step 1: Preparation
- Step 2: Running Automated Migrations
- Step 3: Manual Upgrades and Checks
- Step 4: API Migration
- Handling Major Changes
- Additional Resources
- Troubleshooting
- Conclusion
Introduction
Strapi 5 brings several improvements, including database agnosticism, enhanced performance, a new content release system, and more. While the upgrade process involves several steps, Strapi provides tools to automate much of the work.
Prerequisites
Before beginning the upgrade process, ensure that:
- Your Strapi v4 application is running on the latest v4 minor and patch version. If not, run:
npx @strapi/upgrade minor
- You have backed up your database.
- You have backed up your code (preferably in a new git branch).
- All plugins you’re using are compatible with Strapi 5 (check the Strapi Marketplace for compatibility).
Step-by-Step Upgrade Process
Step 1: Preparation
- Backup your database:
- For SQLite (default): Backup the
data.db
file in the.tmp/
folder. - For PostgreSQL or MySQL: Refer to their official documentation for backup procedures.
- For Strapi Cloud users: Create a manual backup through the dashboard.
- Backup your code:
- If using git: Create a new branch for the upgrade.
- If not using git: Create a copy of your entire Strapi v4 project.
- Check plugin compatibility:
- Review each plugin you’re using and check its compatibility with Strapi 5 on the Marketplace.
Step 2: Running Automated Migrations
Strapi provides an upgrade tool to automate much of the process:
- Run the upgrade command:
npx @strapi/upgrade major
- This command will:
- Update and install dependencies for Strapi 5.
- Run codemods to handle some breaking changes.
- The codemods will automatically handle several changes, including:
- Commenting out lifecycle files
- Removing the i18n plugin dependency
- Upgrading React and related dependencies
- Partly migrating from Entity Service API to Document Service API
- Updating various imports and configurations
- After the automated process, look for
__TODO__
comments in your code. These indicate areas where manual intervention might be needed, especially in the migration from Entity Service API to Document Service API.
Step 3: Manual Upgrades and Checks
- Database Migration:
- Ensure your database is compatible. MySQL v5 is no longer supported.
- If using SQLite, only
better-sqlite3
is now supported. - For MySQL users, only
mysql2
is supported now.
- Configuration Changes:
- Review your environment variables. Some may now be handled by server configuration.
- Ensure custom configurations meet the new requirements in Strapi 5.
- Admin Panel Customization:
- The
helper-plugin
has been removed. Review any custom admin panel code that used this.
- Lifecycle Hooks:
- Lifecycle hooks are triggered differently in Strapi 5. Review and update your hooks accordingly.
- Review Breaking Changes:
- Go through the breaking changes database to check for any other changes that might affect your specific setup.
Step 4: API Migration
Both REST and GraphQL APIs have been updated in Strapi 5. Follow these steps to migrate:
- For REST API:
- Enable retro-compatibility by setting the
Strapi-Response-Format: v4
header. - Update your queries and mutations based on the REST API breaking changes.
- Test your client thoroughly.
- Once everything works, remove the retro-compatibility header to use the new format.
- For GraphQL API:
- Enable retro-compatibility by setting
v4ComptabilityMode: true
in thegraphql.config
object (/config/plugins.js|ts
). - Update queries and mutations following the GraphQL breaking changes.
- Test your client thoroughly.
- Disable retro-compatibility mode when ready.
Handling Major Changes
- Document Service API:
- This new API replaces the Entity Service API. Refer to the migration guide for detailed instructions on transitioning.
- Helper Plugin Removal:
- If you’ve developed custom plugins, you’ll need to refactor code that used the helper-plugin. Consult the helper-plugin deprecation reference for guidance.
Additional Resources
- Entity Service API to Document Service API migration reference
- Plugins upgrade summary
- Helper-plugin deprecation reference
Troubleshooting
If you encounter issues during the upgrade process:
- Check the Strapi 5 documentation for known issues and solutions.
- Review the automatically added
__TODO__
comments in your code for guidance. - Consult the Strapi community forums or GitHub issues for similar problems and solutions.
- If all else fails, consider rolling back to your backup and seeking help from the Strapi community or support channels.
Conclusion
Upgrading to Strapi 5 brings numerous benefits and new features to your headless CMS. While the process involves several steps, the automated tools and clear migration paths provided by Strapi make it manageable. Remember to test thoroughly at each stage of the upgrade process and leverage the retro-compatibility features to ensure a smooth transition for your projects.
By following this guide and utilizing the additional resources provided, you should be able to successfully upgrade your Strapi v4 application to Strapi 5, unlocking new capabilities and improved performance for your content management needs.
Happy upgrading!
Hanzala — Software Developer🎓
Thank you for reading until the end. Before you go:
- Follow me X | LinkedIn | Facebook | GitHub
- Reach Out hi@hanzala.co.in