Deploying a SvelteKit App
SvelteKit is a modern framework for building high-performance web applications with Svelte. It offers file-based routing, server-side rendering, static site generation, and a flexible adapter system, making it ideal for building fast, scalable, and maintainable web projects.
This guide explains how to deploy a SvelteKit application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- Node.js 18+
- Git and GitHub account
- Klutch.sh account
Getting Started: Install SvelteKit
- Create a new SvelteKit app:
Terminal window npm create svelte@latest my-sveltekit-appcd my-sveltekit-appnpm install - Start the development server:
Your app should be running at http://localhost:5173 or http://localhost:3000.
Terminal window npm run dev
Sample Code (src/routes/+page.svelte)
Add a simple page to your SvelteKit project:
<script> // No script needed for this simple example</script>
<h1>Hello from SvelteKit on Klutch.sh!</h1><p>This is your starter page.</p>
Deploying Without a Dockerfile
- Push your SvelteKit app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your SvelteKit GitHub repository and branch
- Set the port to route traffic (usually 3000 for SvelteKit)
- Choose region, compute, number of instances, and add any environment variables
- Add a start script in your
package.json
:"scripts": {"start": "svelte-kit start"} - Make sure your server listens on the port provided by Klutch.sh via the
PORT
environment variable:(If using a custom server. SvelteKit’s default adapter will handle this automatically.)const port = process.env.PORT || 3000;app.listen(port, () => {console.log(`Server running on port ${port}`);}); - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official Node.js imageFROM node:18-alpine# Set working directoryWORKDIR /app# Copy package files and install dependenciesCOPY package*.json ./RUN npm install --production# Copy app sourceCOPY . .# Build SvelteKit appRUN npm run build# Expose port (match your SvelteKit app)EXPOSE 3000# Start the appCMD ["npm", "start"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your SvelteKit app should always listen on the PORT
environment variable as shown above if using a custom server.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.