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
.
- Install
adapter-node
:
npm i @sveltejs/adapter-node --save-dev
- In
svelte.config.js
, change the adapter:
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:
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
Create a Railway
project
.From the terminal in your project folder run:
shrailway init
Select
Empty Project
Set a project name.
Once it's done add a database by running the following command:
shrailway add
Select
postgressql
as the database.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 },
Once that's done run the following command to upload the project to railway:
shrailway 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
- Go to the
railway
project's site and click on the project - Switch to the
settings
tab - Under
Environment
click onGenerate Domain
- Under
Build
change the build from the defaultNixpacks
to theRailpack
- Switch to the
variables
tab - Click on
+ New Variable
, and in theVARIABLE_NAME
clickAdd Reference
and selectDATABASE_URL
- Add another variable called
AUTH_SECRET
and set it to a random string, you can use an online UUID generator - 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.⭐