Elysia app fails to start on railway
kyvrixon
FREEOP

8 months ago

Works locally but doesn't like railways docker environments. I've looked online for a bit and cant find anything conclusive.

Error message: WebStandard does not support listen, you might want to export default Elysia.fetch instead.

I have indeed exported as suggested but the process just dies since it is now treated as a module then the startup command just loops indefinitely eating up resources

Project ID: 6f683200-7f99-4ad6-9431-ffe6ca564062
Service: API
-# fairly new to elysia as im migrating from expressjs

$10 Bounty

2 Replies

kyvrixon
FREEOP

8 months ago

so after some digging apparently railway is running in serverless mode despite that config being disabled


mrflawlezz
FREE

8 months ago

hey man

This is a really common point of confusion when deploying Elysia.js apps, so don't worry, it's a solvable issue. The problem comes from the difference between how Elysia runs in a server environment like Railway versus a serverless one.

The error message is pushing you in the wrong direction for this platform. Railway needs a long-running server process, which is what app.listen() creates. The export default app.fetch method is for serverless environments where the script runs, exports a function, and then stops.

When you switched to export default app.fetch, your script finished executing immediately, so Railway's health checker saw that no process was listening on the required port, marked it as unhealthy, and restarted it. This is what's causing the infinite loop and high resource usage.

The correct solution is to use app.listen() but with the right configuration for a container.

Here is the code you should use in your main server file (e.g., src/index.ts):

import { Elysia } from 'elysia';

const app = new Elysia()

.get('/', () => 'Hello from Elysia on Railway!');

// Add your other routes here

// Railway provides a PORT environment variable.

// We fall back to 3000 for local development.

const port = process.env.PORT || 3000;

app.listen({

// This is required for Docker/container environments.

// It tells the app to listen on all available network interfaces.

hostname: '0.0.0.0',

port: port

}, () => {

console.log(`white_check_mark emoji Elysia is running on port ${port}...`);

});

Quick Checklist to Fix This:

* Update Your Code: Replace your current export default app.fetch or app.listen() with the code block above. The two most important parts are port: process.env.PORT and hostname: '0.0.0.0'.

* Check package.json: Make sure your start script is pointing to the correct file. For example:

"scripts": {

"start": "bun src/index.ts"

}

* Check Railway Start Command: In your service's settings on Railway, ensure your "Start Command" is npm run start or bun run start.

* Redeploy: Trigger a new deployment. If it's successful, you should see the white_check_mark emoji Elysia is running on port... message in your deployment logs.

This should resolve the issue completely.


Loading...