|Docs

Deploy a SvelteKit App

deploymentfrontendsveltekitfullstacksvelte

SvelteKit is a framework for rapidly developing robust, performant web applications using Svelte.

This guide covers how to deploy a SvelteKit app to Railway in four ways:

  1. One-click deploy from a template.
  2. From a GitHub repository.
  3. Using the CLI.
  4. Using a Dockerfile.

Now, let's create a SvelteKit app!

Create a SvelteKit app

Note: If you already have a SvelteKit app locally or on GitHub, you can skip this step and go straight to the Deploy SvelteKit App to Railway.

To create a new SvelteKit app, ensure that you have Node installed on your machine.

Run the following command in your terminal to create a new SvelteKit app using Vite:

npx sv create svelteapp --add sveltekit-adapter="adapter:node" --install npm

Follow the prompts:

  1. Select the SvelteKit demo template.
  2. Add type checking with TypeScript syntax.
  3. Add prettier, eslint, and tailwindcss.
  4. No tailwindcss plugins. Hit enter and move on.

A new SvelteKit app will be provisioned for you in the svelteapp directory.

Run the SvelteKit app locally

Next, cd into the directory and start the Vite development server by running the following command:

npm run dev

Open your browser and go to http://localhost:5173 to see the app. You can play the demo game by visiting the /sverdle route.

Prepare SvelteKit app for deployment

Your app has been configured to use the SvelteKit Node adapter. It takes your app's build output and generates a standalone Node server.

Next, add the start script to the package.json file.

Svelte builds your project into a build directory. The server starts when the server entry point is executed, which is by default located at build/index.js.

Open up the package.json file and add the start script. Set it to node build like so:

{
	"name": "svelteapp",
	"private": true,
	"version": "0.0.1",
	"type": "module",
	"scripts": {
		"start": "node build",

package.json

Now you're ready to deploy!

Deploy the SvelteKit app to Railway

Railway offers multiple ways to deploy your SvelteKit app, depending on your setup and preference.

One-click deploy from a template

If you’re looking for the fastest way to get started, the one-click deploy option is ideal.

Click the button below to begin:

Deploy on Railway

It is highly recommended that you eject from the template after deployment to create a copy of the repo on your GitHub account.

Note: You can also choose from a variety of Svelte app templates created by the community.

Deploy from the CLI

  1. Install the Railway CLI:
  2. Initialize a Railway Project:
    • Run the command below in your SvelteKit app directory.
      railway init
    • Follow the prompts to name your project.
    • After the project is created, click the provided link to view it in your browser.
  3. Deploy the Application:
    • Use the command below to deploy your app:
      railway up
    • This command will scan, compress and upload your app's files to Railway. You’ll see real-time deployment logs in your terminal.
    • Once the deployment completes, go to View logs to check if the service is running successfully.
  4. Set Up a Public URL:
    • Navigate to the Networking section under the Settings tab of your new service.
    • Click Generate Domain to create a public URL for your app.
screenshot of the deployed SvelteKit service

Deploy from a GitHub repo

To deploy a SvelteKit app to Railway directly from GitHub, follow the steps below:

  1. Create a New Project on Railway:
    • Go to Railway to create a new project.
  2. Deploy from GitHub:
    • Select Deploy from GitHub repo and choose your repository.
      • If your Railway account isn’t linked to GitHub yet, you’ll be prompted to do so.
  3. Deploy the App:
    • Click Deploy to start the deployment process.
    • Once the deployed, a Railway service will be created for your app, but it won’t be publicly accessible by default.
  4. Verify the Deployment:
    • Once the deployment completes, go to View logs to check if the server is running successfully.
  5. Set Up a Public URL:
    • Navigate to the Networking section under the Settings tab of your new service.
    • Click Generate Domain to create a public URL for your app.

Use a Dockerfile

  1. Create a Dockerfile in the SvelteKit app's root directory.

  2. Add the content below to the Dockerfile:

    # Use the Node alpine official image
    # https://hub.docker.com/_/node
    FROM node:lts-alpine
    
    # Create and change to the app directory.
    WORKDIR /app
    
    # Copy the files to the container image
    COPY package*.json ./
    
    # Install packages
    RUN npm ci
    
    # Copy local code to the container image.
    COPY . ./
    
    # Build the app.
    RUN npm run build
    
    # Serve the app
    CMD ["npm", "run", "start"]
  3. Either deploy via the CLI or from GitHub.

Railway automatically detects the Dockerfile, and uses it to build and deploy the app.

Note: Railway supports also deployment from public and private Docker images.

This guide covers the main deployment options on Railway. Choose the approach that suits your setup, and start deploying your SvelteKit apps seamlessly!

Next steps

Explore these resources to learn how you can maximize your experience with Railway: