This guide will walk you through creating a new SvelteKit project and integrating CreativeEditor SDK (CE.SDK) using a custom component. By the end, you’ll have a SvelteKit application with a fully functional CE.SDK component, ready for further customization.
Who Is This Tutorial For?
This guide is for developers who:
- Are familiar with SvelteKit.
- Need to set up a new SvelteKit project from scratch.
- Want to incorporate a robust image and video editing component into that SvelteKit application.
What You’ll Achieve
- Set up a new SvelteKit project using the Svelte CLI.
- Install CE.SDK via NPM.
- Create a custom Svelte component for CE.SDK with default settings.
- Render the CE.SDK component within your application.
Prerequisites
Before starting, ensure you have the following:
- Node.js v20+ and NPM 10+ installed. Download the latest LTS version of Node.js and NPM.
- A valid CE.SDK license key (begin a free trial to get one).
Step 1: Initialize a New SvelteKit Project
Create a new SvelteKit project named my-sveltekit-app using the Svelte CLI:
npx sv create my-sveltekit-app
You’ll be prompted with a few setup questions. In this tutorial, we’ll refer to a minimal SvelteKit app in plain JavaScript and with npm as the package manager.
Once the project is created, navigate into the project folder:
cd my-sveltekit-app
The SvelteKit project should contain a file structure as below:
my-sveltekit-app/│├── src # Source code│ ├── app.html # Main HTML file for the app│ ├── lib # Library files│ │ └── index.js # Entry point for the lib folder│ └── routes # Application routes│ └── +page.svelte # Svelte page component (default route)│├── static # Static assets│ └── favicon.png│├── .gitignore # Git ignore rules├── .npmrc # NPM configuration├── jsconfig.json # JavaScript project config├── package-lock.json # NPM dependency lock file├── package.json # Project metadata and dependencies├── README.md # Project documentation├── svelte.config.js # Svelte configuration└── vite.config.js # Vite configuration
Install the project dependencies with:
npm install
Step 2: Install CE.SDK
Add CreativeEditor SDK to your project’s dependencies. Installing it via the @cesdk/cesdk-js
NPM package:
npm install @cesdk/cesdk-js
Step 3: Define the Creative Editor Svelte Component
In the src/lib/
folder of your new SvelteKit project, add a new file named CreativeEditorSDK.svelte
. Define it as follows:
<script> import CreativeEditorSDK from '@cesdk/cesdk-js'; import { onDestroy, onMount } from 'svelte';
// reference to the container HTML element where CE.SDK will be initialized let container; // where to keep track of the CE.SDK instance let cesdk = null;
// deafult CreativeEditor SDK configuration const defaultConfig = { license: '<YOUR_LICENSE_KEY>', // replace it with a valid CE.SDK license key callbacks: { onUpload: 'local' }, // enable local file uploads in the Asset Library // other default configs... };
// accessing the component's props const { el, children, class: _, config, ...props } = $props();
// hook to initialize the CreativeEditorSDK component onMount(() => { // integrate the configs read from props with the default ones const ceSDKConfig = { ...defaultConfig, ...config, };
try { // initialize the CreativeEditorSDK instance in the container element // using the given config CreativeEditorSDK.create(container, ceSDKConfig).then(async instance => { cesdk = instance;
// do something with the instance of CreativeEditor SDK (e.g., populate // the asset library with default / demo asset sources) await Promise.all([ cesdk.addDefaultAssetSources(), cesdk.addDemoAssetSources({ sceneMode: 'Design' }), ]);
// create a new design scene in the editor await cesdk.createDesignScene(); }); } catch (err) { console.warn(`CreativeEditor SDK failed to mount.`, { err }); } });
// hook to clean up when the component unmounts onDestroy(() => { try { // dispose of the CE.SDK instance if it exists if (cesdk) { cesdk.dispose(); cesdk = null; } } catch (err) { // log error if CreativeEditor SDK fails to unmount console.warn(`CreativeEditor SDK failed to unmount.`, { err }); } });</script>
<!-- the container HTML element where the CE.SDK editor will be mounted --><div id="cesdk_container" bind:this="{container}"></div>
<style> /* styling for the CE.SDK container element to take full viewport size */ #cesdk_container { height: 100vh; width: 100vw; }</style>
This Svelte component loads the CreativeEditor SDK instance within a <div>
container element defined in the component’s HTML. When the component is unmounted, the CE.SDK instance is correctly disposed of to release resources.
To simplify the import of the CreativeEditorSDK.svelte
component, export it in the index.js
file inside the src/lib/
folder:
export { default as CreativeEditorSDK } from './CreativeEditorSDK.svelte';
Step 4: Use the Creative Editor Component
CreativeEditor SDK must be used on the client to work. In your SSR src/routes/+page.svelte
route file, dynamically import the CreativeEditorSDK.svelte
component inside the <script>
section:
import { browser } from '$app/environment'; // true only if the app is running in the browser// use the browser flag to conditionally render client-side componentslet isClient = browser;
let CreativeEditorSDK;if (isClient) { // dynamically import the CreativeEditorSDK component only in the browser import('$lib').then(module => { CreativeEditorSDK = module.CreativeEditorSDK; });}
Note: browser
from $app/environment
is a special flag that is true only when the application is rendered in the browser.
Now, conditionally render the video editor component on the client side. Add this to the template section of your SvelteKit route:
{#if isClient && CreativeEditorSDK} <CreativeEditorSDK config={{ // Your custom configs here }} />{/if}
Or, if you don’t need custom configurations, use:
{#if isClient && CreativeEditorSDK}<CreativeEditorSDK />{/if}
src/routes/+page.svelte
will contain something like:
<script> // other imports...
import { browser } from "$app/environment"; // true only if the app is running in the browser // use the browser flag to conditionally render client-side components let isClient = browser;
let CreativeEditorSDK; if (isClient) { // dynamically import the CreativeEditorSDK component only in the browser import("$lib").then(module => { CreativeEditorSDK = module.CreativeEditorSDK; }); }</script>
<main> <!-- other components... --> {#if isClient && CreativeEditorSDK} <CreativeEditorSDK config={{ // custom configs... }} /> {/if} <!-- other components... --></main>
<style> /* custom styling... */</style>
Step 5: Serve the SvelteKit Project Locally
Run the project locally with the following command:
npm run dev
By default, the SvelteKit app will be served with Vite and be available on your localhost at http://localhost:5173/
.
Step 6: Test the Integration
- Open
http://localhost:5173/
in your browser. - A fully functional CE.SDK editor should load.
Troubleshooting & Common Errors
❌ Error: Cannot find module '@cesdk/cesdk-js'
- Verify that you’ve correctly installed CE.SDK via
npm install @cesdk/cesdk-js
.
❌ Error: The $props rune is only available inside .svelte and .svelte.js/ts files
- Make sure that you’re loading the props of the
CreativeEditorSDK.svelte
component using$props()
in the top-level<script>
section, rather than inside onMount() or other lifecycle methods.
❌ Error: Error when evaluating SSR module /src/routes/+page.svelte: document is not defined
- Ensure that you’re importing the
CreativeEditorSDK
Svelte component dynamically in/src/routes/+page.svelte
and conditionally rendering it only on the client side.
❌ Error: Editor engine could not be loaded: The License Key (API Key) you are using to access CE.SDK is invalid
- Double-check that your license key is valid and hasn’t expired.
❌ Editor does not load
- Inspect the browser console for any errors.
- Ensure that your component paths and imports are correct.
Next Steps
Great job! You’ve successfully integrated CE.SDK into a new SvelteKit project. Now, feel free to explore the SDK and proceed to the next steps whenever you’re ready: