Skip to content

Deployment

Let's deploy the todo app to railway.app.

Prepare for Production

In order to deploy to a Node.js environment, you need to change Sveltekit's adaptor to @sveltejs/adapter-node.

  1. Install adapter-node:
sh
npm i @sveltejs/adapter-node --save-dev
  1. In svelte.config.js, change the adapter:
js
import adapter from '@sveltejs/adapter-auto'
import adapter from '@sveltejs/adapter-node'

You also need to change the dataProvider on the remult initilizer a little bit. By default Sveltekit will try to access your database when running the npm run build command, but on railway, the postgres database is not accessible at this time, and it will make your deployment fail.

To solve this, we need to make Sveltekit use the default JSON database when building, and use Postgres only in production.

Make the following changes on your server/api.ts file:

ts
import { remultApi } from 'remult/remult-sveltekit'
import { Task } from './shared/Task'
import { TasksController } from './shared/TasksController'
import { createPostgresDataProvider } from 'remult/postgres' 
import { DATABASE_URL } from '$env/static/private'
import { building } from '$app/environment';

export const api = remultApi({
  entities: [Task],
  controllers: [TasksController],
  dataProvider: DATABASE_URL
    ? createPostgresDataProvider({ connectionString: DATABASE_URL })
    : undefined,
   dataProvider: async () => { 
		if (DATABASE_URL && !building) { 
			return createPostgresDataProvider({ 
				connectionString: DATABASE_URL
			});
		} 
		return undefined;
	},
  getUser: async (event) => {
    const auth = await event?.locals?.auth()
    return auth?.user as UserInfo
  },
})

In order to deploy the todo app to railway you'll need a railway account. You'll also need Railway CLI installed, and you'll need to login to railway from the cli, using railway login.

Click enter multiple times to answer all its questions with the default answer

  1. Create a Railway project.

    From the terminal in your project folder run:

    sh
    railway init
  2. Select Empty Project

  3. Set a project name.

  4. Once it's done add a database by running the following command:

    sh
    railway add
  5. Select postgressql as the database.

  6. You need to modify your package.json file and tell the railway to use the correct version of NodeJS, like this:

    jsonc
     "type": "module",
    "engines": {
     	"node": ">=20.19"
     }, 
    "scripts": {
     	// ... your scripts
     },
  7. Once that's done run the following command to upload the project to railway:

    sh
    railway up

Note

Due to a bug in the way the default Railway builds, the first time you use the railway up command, it will fail to deploy. Continue to follow the steps to fix it

  1. Go to the railway project's site and click on the project
  2. Switch to the settings tab
  3. Under Environment click on Generate Domain
  4. Under Build change the build from the default Nixpacks to the Railpack
  5. Switch to the variables tab
  6. Click on + New Variable, and in the VARIABLE_NAME click Add Reference and select DATABASE_URL
  7. Add another variable called AUTH_SECRET and set it to a random string, you can use an online UUID generator
  8. Wait for railway to finish deploying your changes and Click on the newly generated url to open the app in the browser and you'll see the app live in production. (it may take a few minutes to go live)

Note

If you run into trouble deploying the app to Railway, try using Railway's documentation.

That's it - our application is deployed to production, on a node js server


Love Remult?  Give our repo a star.⭐

MIT Licensed | Made by the Remult team with ❤️