The error you're encountering:
Uncaught CKEditorError: ckeditor-duplicated-modules
Read more: https://ckeditor.com/docs/ckeditor5/latest/support/error-codes.html#error-ckeditor-duplicated-modules
at version.js:151:11
at texttransformation.js:147:5
at texttransformation.js:147:5
at universalModuleDefinition:9:27
at universalModuleDefinition:1:1
indicates that multiple instances or copies of CKEditor 5 modules are being loaded in your project. This duplication can lead to unexpected behaviors, increased bundle sizes, and other runtime errors.
Understanding the Error
The ckeditor-duplicated-modules error is thrown by CKEditor 5 to prevent issues arising from having more than one instance of its modules loaded simultaneously. This typically happens when:
- Multiple Builds: Loading different builds of CKEditor or custom builds alongside each other.
- Dependency Duplication: Having CKEditor packages listed multiple times in your
node_modules due to version mismatches or improper dependency management.
- Bundler Configuration Issues: Incorrect configuration in bundlers like Webpack, Rollup, or others that lead to CKEditor modules being included more than once in the final bundle.
Common Causes and Solutions
1. Multiple CKEditor Builds
Cause: Including more than one build of CKEditor (e.g., Classic, Inline, Balloon) or mixing builds with custom builds.
Solution:
- Use a Single Build: Ensure you are using only one CKEditor build per project.
- Avoid Mixing Builds: If you need features from different builds, consider creating a custom build that includes all required plugins.
Example:
// Instead of importing multiple builds
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import InlineEditor from '@ckeditor/ckeditor5-build-inline'; // Remove this if not needed
// Use only one editor build
const editor = ClassicEditor.create(document.querySelector('#editor'));
2. Dependency Duplication
Cause: Multiple versions of CKEditor packages are installed due to dependencies in package.json, leading to duplicated modules in node_modules.
Solution:
- Check for Duplicates: Use tools like
npm ls @ckeditor/ckeditor5-* or yarn why @ckeditor/ckeditor5-* to identify duplicated packages.
- Align Versions: Ensure all CKEditor packages in your project use the same version.
- Use Resolutions (Yarn): Force all CKEditor packages to resolve to the same version using the
resolutions field in package.json.
Example (using Yarn):
{
"resolutions": {
"@ckeditor/ckeditor5-*": "35.3.3" // Replace with your CKEditor version
}
}
After adding resolutions, run:
yarn install
3. Bundler Configuration Issues
Cause: Incorrect bundler settings causing CKEditor modules to be included multiple times.
Solution for Webpack:
- Use
resolve.alias: Ensure that all CKEditor imports resolve to a single path.
Example:
// webpack.config.js
const path = require('path');
module.exports = {
// ... other configurations
resolve: {
alias: {
'@ckeditor': path.resolve(__dirname, 'node_modules/@ckeditor/')
}
}
};
- Avoid Multiple Imports: Ensure that CKEditor modules are imported consistently throughout your project.
Solution for Other Bundlers:
- Rollup: Use the
@rollup/plugin-alias to set up similar aliases.
- Vite: Configure the
resolve.alias option in vite.config.js.
4. External Dependencies Loading CKEditor Separately
Cause: Dependencies or external scripts load their own instances of CKEditor, leading to multiple copies.
Solution:
- Consolidate Imports: Make sure that all parts of your application use the same CKEditor instance.
- Peer Dependencies: If developing a library, declare CKEditor as a peer dependency to prevent bundling duplicates.
Step-by-Step Guide to Resolve the Issue
-
Identify Duplicates:
- Run
npm ls @ckeditor/ckeditor5-* or yarn list @ckeditor/ckeditor5-* to see if multiple versions are installed.
- Look for multiple instances of CKEditor packages in the output.
-
Align Package Versions:
-
Clean Installation:
- Delete
node_modules and lock files (package-lock.json or yarn.lock).
- Reinstall dependencies:
npm install
or
yarn install
-
Configure Bundler:
- If using Webpack, set up aliases to ensure a single path resolution.
- Check your bundler's configuration to prevent multiple inclusions.
-
Check Third-Party Integrations:
- Ensure that any third-party libraries or plugins are not bringing their own CKEditor instances.
- If they do, consider using peer dependencies or refactoring to use a single instance.
-
Test the Application:
- After making the changes, rebuild your project.
- Verify that the error no longer appears.
Additional Recommendations
-
Use a Custom Build: If you require specific plugins or configurations, consider creating a CKEditor 5 custom build to have better control over the included modules.
-
Monitor Package Updates: Keep CKEditor packages updated to benefit from bug fixes and improvements. However, ensure consistency across all related packages when updating.
-
Consult Official Documentation and Support:
By following these steps, you should be able to identify and eliminate the duplication of CKEditor modules in your project, resolving the ckeditor-duplicated-modules error.