Resolve the "Unable to resolve dependency" error in Docusaurus v3 by explicitly aligning your react and react-dom versions with Docusaurus requirements in package.json, then clearing the lock file and reinstalling dependencies using npm install or yarn.
Understanding the Dependency Conflict
Docusaurus v3 is built on modern React patterns, specifically requiring React 18. When upgrading from an older version of Docusaurus or migrating a legacy project, developers often encounter resolution errors because the package manager detects a mismatch between the React version specified in your project’spackage.json and the peer dependencies required by Docusaurus core packages. This conflict typically manifests as an error message stating that it cannot resolve a dependency tree, often citing that react@^17.0.0 is incompatible with @docusaurus/core@^3.0.0.
The root cause is usually twofold. First, the package.json file may still list React 16 or 17 as a direct dependency. Second, the lock file (package-lock.json for npm or yarn.lock for Yarn) contains cached resolutions from the previous installation that prevent the package manager from updating to the newer versions even if you manually edit package.json. Package managers prioritize the lock file to ensure deterministic builds, meaning manual edits are ignored unless the lock file is regenerated.
Step-by-Step Resolution Process
To fix this issue, you must force the package manager to discard old resolutions and fetch the correct versions. Follow these steps carefully to ensure a clean upgrade.- Update
package.jsonDependencies: Open your project’spackage.jsonfile. Locate thedependenciessection. Ensure thatreactandreact-domare set to version18.x.x. For example:
If you are using other React-based libraries, such as AntD, check their compatibility with React 18. As noted in related guides on Limit File Upload and Display Message in React with AntD, ensuring library compatibility is crucial before upgrading the core framework."dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0", "@docusaurus/core": "3.0.0", "@docusaurus/preset-classic": "3.0.0" } - Delete the Lock File: This is the most critical step. Delete
package-lock.jsonif you use npm, oryarn.lockif you use Yarn. Additionally, delete thenode_modulesfolder. This ensures that no stale references remain in the cache.# For npm users rm -rf node_modules package-lock.json # For yarn users rm -rf node_modules yarn.lock - Reinstall Dependencies: Run the installation command again. This forces the package manager to build a new dependency tree based on the updated
package.json.npm install # or yarn install - Verify the Installation: After installation completes, run
npm ls reactoryarn why reactto verify that the installed version is indeed 18.x.x. If the output shows version 17 or lower, there may be a transitive dependency forcing the older version.
Handling Transitive Dependency Issues
Sometimes, even after updatingpackage.json and deleting the lock file, the error persists. This usually happens because a third-party package in your dependency tree still requires an older version of React. To diagnose this, look at the detailed error log provided by npm or Yarn. It will often specify which package is requesting the conflicting version.
If a specific plugin or theme is causing the conflict, you may need to update that plugin to a version compatible with Docusaurus v3. Check the documentation for each custom plugin or theme you have installed. If a plugin has not been updated to support React 18, you might need to find an alternative or fork the repository to make it compatible. In some cases, you can use the overrides field in package.json (npm) or resolutions field (Yarn) to force a specific version, though this should be used as a last resort due to potential runtime bugs.
For projects involving complex data fetching, ensure that your HTTP clients are also compatible. Issues like React Axios Call Pending After Multiple Requests can sometimes arise during upgrades if state management libraries are not properly synchronized with the new React lifecycle methods. Always test your API calls after the upgrade to ensure stability.
Common Pitfalls and Best Practices
One common mistake is forgetting to update thedocusaurus.config.js file. While this does not directly cause dependency resolution errors, Docusaurus v3 introduces configuration changes that can break the build if the old syntax is used. Review the official migration guide for any changes in plugin configurations.
Another pitfall is using global installations of Docusaurus CLI. It is recommended to install Docusaurus locally within the project directory to avoid version mismatches between the global CLI and the local project dependencies. Use npx docusaurus --version to check the local version rather than relying on global installs.
Finally, always commit your changes to version control before performing major upgrades. This allows you to revert if the upgrade introduces breaking changes in unexpected areas. Regularly auditing your dependencies using tools like npm audit can help identify security vulnerabilities and outdated packages before they become blockers during major version upgrades.
Frequently Asked Questions
Why does deleting the lock file fix the error?
The lock file caches the exact versions of all dependencies resolved during the last install. Deleting it forces the package manager to re-evaluate the dependency tree against the current package.json constraints, allowing it to fetch newer compatible versions.
Can I use React 19 with Docusaurus v3?
Docusaurus v3 officially supports React 18. While React 19 might work in some scenarios, it is not guaranteed to be stable. It is best to stick to React 18 for production environments until Docusaurus releases a version explicitly supporting React 19.
What if a third-party plugin breaks after the upgrade?
If a plugin fails, check its repository for updates compatible with React 18. If none exist, consider finding an alternative plugin or contributing to the existing one to add compatibility. You can also temporarily remove the plugin to isolate the issue.
How do I check which package is causing the conflict?
Run npm ls react or yarn why react in your terminal. The output will show the dependency tree and highlight which packages are depending on different versions of React, helping you identify the source of the conflict.
Should I use npm or Yarn for Docusaurus v3?
Both package managers are supported. However, consistency is key. Stick to the tool you used previously or choose one and use it throughout the project lifecycle to avoid lock file conflicts between npm and Yarn.