bun + turborepo monorepo application failing to respond

heybereket
PRO

a year ago

For some reason my application is saying it's failing to respond, however it successfully deploys (or at least it says so…)

I have PORT configured for Railway's default to be injected, so that isn't the issue. I also setup both my start & build scripts, which are:

Start: bun start --filter=api
Build: bun run build --filter=api

0 Replies

heybereket
PRO

a year ago

aac0388c-812d-41d2-abad-f664d8a4fc9f


a year ago

make sure you are listening on the $PORT environment variable


heybereket
PRO

a year ago

server.listen({ host: process.env.HOST, port: process.env.PORT }, (err, address) => {
    if (err) {
      Logger.error("SERVER", err.message);
      process.exit(1);
    }

    Logger.info("SERVER", `Listening at ${address}`);
  });

heybereket
PRO

a year ago

1254618360361124000


heybereket
PRO

a year ago

seem to be getting this now, weird


a year ago

there is no HOST variable, just use 0.0.0.0


heybereket
PRO

a year ago

I have it set to (where these are under the env cost)


heybereket
PRO

a year ago

HOST: z.string().default("0.0.0.0"),
API_URL: z.string().default("http://localhost:8080"),

heybereket
PRO

a year ago

Sorry, it shouldn't been:

server.listen({ host: env.HOST, port: env.PORT }, (err, address) => {
    if (err) {
      Logger.error("SERVER", err.message);
      process.exit(1);
    }

    Logger.info("SERVER", `Listening at ${address}`);
  });

a year ago

what is PORT set to in zod ?


heybereket
PRO

a year ago

PORT: z.number().default(8080),
HOST: z.string().default("0.0.0.0"),

a year ago

you should use process.env.PORT instead, and if that isn't available then you can default to 8080, not sure what that looks like in zod though


heybereket
PRO

a year ago

will try rn


heybereket
PRO

a year ago

but i doubt thats the issue since it should be defined


a year ago

is it? do you have a PORT service variable?


a year ago

you'd still want to be listening on the PORT environment variable in code though


heybereket
PRO

a year ago

1254621745521102800


heybereket
PRO

a year ago

running into this issue now


heybereket
PRO

a year ago

when i try to deploy those new changes (but anything really)


heybereket
PRO

a year ago

1254621885161934800


heybereket
PRO

a year ago

keeps retrying


a year ago

would you happen to have a custom start command set now?


heybereket
PRO

a year ago

1254621967462568000


heybereket
PRO

a year ago

1254621983140876300


heybereket
PRO

a year ago

the structure of my codebase is:

apps
-> api
package.json

a year ago

do you have the new builder also enabled? the new builder has a bug where it doesn't pass in the start command correctly


heybereket
PRO

a year ago

oh will turn that on


heybereket
PRO

a year ago

nope i dont


heybereket
PRO

a year ago

misread my bad


heybereket
PRO

a year ago

1254622160140501000


a year ago

hmmm well those logs are not helpful, bun never prints anything helpful it seems


a year ago

I assume you can still run the same pair of build and start commands locally without issues?


heybereket
PRO

a year ago

yeah


heybereket
PRO

a year ago

it all works locally


heybereket
PRO

a year ago

same start & build commands


a year ago

even if you deleted the project and did another clone?


heybereket
PRO

a year ago

on railway or git


a year ago

on git, delete the project locally and clone it again


a year ago

what version of bun do you use locally, and what version of bun is railway using for your project? it's always best to have the cloud platform running the same versions of stuff as you run locally


heybereket
PRO

a year ago

this is what my package.json looks like:

{
  "name": "test",
  "private": true,
  "scripts": {
    "build": "turbo build",
    "dev": "turbo dev",
    "start": "turbo start",
    "lint": "turbo lint",
    "format": "prettier --write \"**/*.{ts,tsx,md}\""
  },
  "devDependencies": {
    "prettier": "^3.3.0",
    "turbo": "latest"
  },
  "engines": {
    "node": ">=18"
  },
  "packageManager": "bun@1.0.21",
  "workspaces": [
    "apps/*"
  ],
  "dependencies": {
    "tsc-alias": "^1.8.10"
  }
}

heybereket
PRO

a year ago

i use bun@1.0.21


a year ago

what version does railway use?


heybereket
PRO

a year ago

where can i check that?


a year ago

temporarily change the start command to a command that prints the version of bun


heybereket
PRO

a year ago

alright


heybereket
PRO

a year ago

1254624040493256700


heybereket
PRO

a year ago

ok


heybereket
PRO

a year ago

1254626867206689000


heybereket
PRO

a year ago

used the railway cli


heybereket
PRO

a year ago

thank god i got a bit more context


a year ago

are you on the v2 runtime?


heybereket
PRO

a year ago

yeah


a year ago

also, I the API app not with bun?


heybereket
PRO

a year ago

wdym


a year ago

node dist/index.js


heybereket
PRO

a year ago

1254627588866048000


heybereket
PRO

a year ago

got it up to this point now


heybereket
PRO

a year ago

still getting

Application failed to respond


heybereket
PRO

a year ago

in the api package, my scripts are:

 "scripts": {
    "dev": "tsx watch --clear-screen=false src/index.ts",
    "migrate": "drizzle-kit generate",
    "build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json",
    "start": "node dist/index.js"
  },

a year ago

we are back to this, please make these changes


heybereket
PRO

a year ago

oh shoot


heybereket
PRO

a year ago

forgot to change that back


heybereket
PRO

a year ago

what's the port railway uses by default?


heybereket
PRO

a year ago

just curious


a year ago

it's random, that's why your app needs to listen on the PORT environment variable


a year ago

please make this change


heybereket
PRO

a year ago

doing that rn


heybereket
PRO

a year ago

alright


heybereket
PRO

a year ago

just tried with:


heybereket
PRO

a year ago

  server.listen(
    {
      host: process.env.HOST ?? "0.0.0.0",
      port: process.env.PORT ? Number(process.env.PORT) : 8080,
    },
    (err, address) => {
      if (err) {
        Logger.error("SERVER", err.message);
        process.exit(1);
      }

      Logger.info("SERVER", `Listening at ${address}`);
    }
  );

heybereket
PRO

a year ago

seems to keep using 8080 thought, so PORT must not be defined by railway


heybereket
PRO

a year ago

1254629877358002200


heybereket
PRO

a year ago

wtf


heybereket
PRO

a year ago

am i doing something wrong


heybereket
PRO

a year ago

ok so


heybereket
PRO

a year ago

apparently it works if i just manually add PORT to my env variables


heybereket
PRO

a year ago

lmfao


heybereket
PRO

a year ago

thank you


a year ago

while that will work, and it's by far the simpler way this is not the recorded way


heybereket
PRO

a year ago

okay


heybereket
PRO

a year ago

so after a bit of debugging


heybereket
PRO

a year ago

it seems like dotenv just doesnt want to parse the env variable on railway


heybereket
PRO

a year ago

it works locally though, which is weird


a year ago

dotenv parses .env files


heybereket
PRO

a year ago

yeah


a year ago

railway does not create .env files


a year ago

they inject the variables directly into the environment


heybereket
PRO

a year ago

wdym


heybereket
PRO

a year ago

how would process.env not work?


heybereket
PRO

a year ago

i understand this


heybereket
PRO

a year ago

but not this


a year ago

I don't know how I could clarify further


heybereket
PRO

a year ago

well maybe i can explain what i mean


heybereket
PRO

a year ago

i have variables that i set on railway, like below:

1254647356222079200


heybereket
PRO

a year ago

and im accessing them through process.env


heybereket
PRO

a year ago

but when i test it by console logging, like console.log([process.env.GOOGLE](process.env.GOOGLE)_CLIENT_ID ?? "some google client id"), it doesn't work, and goes to the fallback of some google client id


a year ago

your code is doing something to unset them


heybereket
PRO

a year ago

oh actually


heybereket
PRO

a year ago

i think the issue is that the env variables are located in the root directory


heybereket
PRO

a year ago

and not in apps/api


a year ago

the environment variables are injected into the containers environment, they aren't scoped to any specific directory


a year ago

in a production environment like railway you don't even need to call dotenv


heybereket
PRO

a year ago

this is my src/utils/env.ts:

import "dotenv/config";
import { z } from "zod";

export const env = z
  .object({
    DATABASE_URL: z
      .string()
      .default("default db url goes here"),

    // ... other variables
  })
  .parse(process.env);

heybereket
PRO

a year ago

1254651664061829000


heybereket
PRO

a year ago

I have it set in Railway


heybereket
PRO

a year ago

and in my src/index.ts, all I do is


heybereket
PRO

a year ago

console.log(env.DATABASE_URL)


heybereket
PRO

a year ago

and for some reason it doesn't return the value that I put in railway


heybereket
PRO

a year ago

and instead that fallback of default db url goes here


a year ago

can zod even parse process.env


heybereket
PRO

a year ago

yes


a year ago

haha don't say yes, if it could, it would work


heybereket
PRO

a year ago

have done it before + works locally


a year ago

so you have something missconfigured


heybereket
PRO

a year ago

will try without using zod for it


heybereket
PRO

a year ago

import "dotenv/config";

export const env = {
  DATABASE_URL: process.env.DATABASE_URL || "postgres://butler:butler@127.0.0.1:5432/postgres",
  // ... other variables
};

heybereket
PRO

a year ago

doesn't seem to work with this either


heybereket
PRO

a year ago

the thing is it works fine locally so i can't repro it


a year ago

something you are doing is un-setting them


a year ago

try removing dotenv because as mentioned, you don't need to call it


bun + turborepo monorepo application failing to respond - Railway Help Station