bun + turborepo monorepo application failing to respond

heybereketPRO

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

heybereketPRO

a year ago

aac0388c-812d-41d2-abad-f664d8a4fc9f


a year ago

make sure you are listening on the $PORT environment variable


heybereketPRO

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}`);
  });

heybereketPRO

a year ago

1254618360361124000


heybereketPRO

a year ago

seem to be getting this now, weird


a year ago

there is no HOST variable, just use 0.0.0.0


heybereketPRO

a year ago

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


heybereketPRO

a year ago

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

heybereketPRO

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 ?


heybereketPRO

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


heybereketPRO

a year ago

will try rn


heybereketPRO

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


heybereketPRO

a year ago

1254621745521102800


heybereketPRO

a year ago

running into this issue now


heybereketPRO

a year ago

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


heybereketPRO

a year ago

1254621885161934800


heybereketPRO

a year ago

keeps retrying


a year ago

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


heybereketPRO

a year ago

1254621967462568000


heybereketPRO

a year ago

1254621983140876300


heybereketPRO

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


heybereketPRO

a year ago

oh will turn that on


heybereketPRO

a year ago

nope i dont


heybereketPRO

a year ago

misread my bad


heybereketPRO

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?


heybereketPRO

a year ago

yeah


heybereketPRO

a year ago

it all works locally


heybereketPRO

a year ago

same start & build commands


a year ago

even if you deleted the project and did another clone?


heybereketPRO

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


heybereketPRO

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"
  }
}

heybereketPRO

a year ago

i use bun@1.0.21


a year ago

what version does railway use?


heybereketPRO

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


heybereketPRO

a year ago

alright


heybereketPRO

a year ago

1254624040493256700


heybereketPRO

a year ago

ok


heybereketPRO

a year ago

1254626867206689000


heybereketPRO

a year ago

used the railway cli


heybereketPRO

a year ago

thank god i got a bit more context


a year ago

are you on the v2 runtime?


heybereketPRO

a year ago

yeah


a year ago

also, I the API app not with bun?


heybereketPRO

a year ago

wdym


a year ago

node dist/index.js


heybereketPRO

a year ago

1254627588866048000


heybereketPRO

a year ago

got it up to this point now


heybereketPRO

a year ago

still getting

Application failed to respond


heybereketPRO

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


heybereketPRO

a year ago

oh shoot


heybereketPRO

a year ago

forgot to change that back


heybereketPRO

a year ago

what's the port railway uses by default?


heybereketPRO

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


heybereketPRO

a year ago

doing that rn


heybereketPRO

a year ago

alright


heybereketPRO

a year ago

just tried with:


heybereketPRO

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}`);
    }
  );

heybereketPRO

a year ago

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


heybereketPRO

a year ago

1254629877358002200


heybereketPRO

a year ago

wtf


heybereketPRO

a year ago

am i doing something wrong


heybereketPRO

a year ago

ok so


heybereketPRO

a year ago

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


heybereketPRO

a year ago

lmfao


heybereketPRO

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


heybereketPRO

a year ago

okay


heybereketPRO

a year ago

so after a bit of debugging


heybereketPRO

a year ago

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


heybereketPRO

a year ago

it works locally though, which is weird


a year ago

dotenv parses .env files


heybereketPRO

a year ago

yeah


a year ago

railway does not create .env files


a year ago

they inject the variables directly into the environment


heybereketPRO

a year ago

wdym


heybereketPRO

a year ago

how would process.env not work?


heybereketPRO

a year ago

i understand this


heybereketPRO

a year ago

but not this


a year ago

I don't know how I could clarify further


heybereketPRO

a year ago

well maybe i can explain what i mean


heybereketPRO

a year ago

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

1254647356222079200


heybereketPRO

a year ago

and im accessing them through process.env


heybereketPRO

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


heybereketPRO

a year ago

oh actually


heybereketPRO

a year ago

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


heybereketPRO

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


heybereketPRO

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);

heybereketPRO

a year ago

1254651664061829000


heybereketPRO

a year ago

I have it set in Railway


heybereketPRO

a year ago

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


heybereketPRO

a year ago

console.log(env.DATABASE_URL)


heybereketPRO

a year ago

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


heybereketPRO

a year ago

and instead that fallback of default db url goes here


a year ago

can zod even parse process.env


heybereketPRO

a year ago

yes


a year ago

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


heybereketPRO

a year ago

have done it before + works locally


a year ago

so you have something missconfigured


heybereketPRO

a year ago

will try without using zod for it


heybereketPRO

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
};

heybereketPRO

a year ago

doesn't seem to work with this either


heybereketPRO

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