Step-by-step guide to upgrade v4 to Strapi v5

alternative
alternative

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

  1. Introduction
  2. Prerequisites
  3. Step-by-Step Upgrade Process
    • Step 1: Preparation
    • Step 2: Running Automated Migrations
    • Step 3: Manual Upgrades and Checks
    • Step 4: API Migration
  4. Handling Major Changes
  5. Additional Resources
  6. Troubleshooting
  7. 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.

Official Strapi Community: Watch the video to know more about what’s new!

Prerequisites

Before beginning the upgrade process, ensure that:

  1. Your Strapi v4 application is running on the latest v4 minor and patch version. If not, run:
   npx @strapi/upgrade minor
  1. You have backed up your database.
  2. You have backed up your code (preferably in a new git branch).
  3. All plugins you’re using are compatible with Strapi 5 (check the Strapi Marketplace for compatibility).

Step-by-Step Upgrade Process

Step 1: Preparation

  1. 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.
  1. 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.
  1. 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:

  1. Run the upgrade command:
   npx @strapi/upgrade major
  1. This command will:
  • Update and install dependencies for Strapi 5.
  • Run codemods to handle some breaking changes.
  1. 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
  1. 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

  1. 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.
  1. Configuration Changes:
  • Review your environment variables. Some may now be handled by server configuration.
  • Ensure custom configurations meet the new requirements in Strapi 5.
  1. Admin Panel Customization:
  • The helper-plugin has been removed. Review any custom admin panel code that used this.
  1. Lifecycle Hooks:
  • Lifecycle hooks are triggered differently in Strapi 5. Review and update your hooks accordingly.
  1. Review Breaking Changes:

Step 4: API Migration

Both REST and GraphQL APIs have been updated in Strapi 5. Follow these steps to migrate:

  1. 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.
  1. For GraphQL API:
  • Enable retro-compatibility by setting v4ComptabilityMode: true in the graphql.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

  1. Document Service API:
  • This new API replaces the Entity Service API. Refer to the migration guide for detailed instructions on transitioning.
  1. Helper Plugin Removal:

Additional Resources

Troubleshooting

If you encounter issues during the upgrade process:

  1. Check the Strapi 5 documentation for known issues and solutions.
  2. Review the automatically added __TODO__ comments in your code for guidance.
  3. Consult the Strapi community forums or GitHub issues for similar problems and solutions.
  4. 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:


Recent Blogs

See All

Let's Build Together

Ready to turn your idea into reality?
Schedule a chat with me.

Hanzala - Self-Taught Developer