R
Railway6mo ago
bereket

bun + turborepo monorepo application failing to respond

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
No description
No description
Solution:
something you are doing is un-setting them
Jump to solution
75 Replies
Percy
Percy6mo ago
Project ID: aac0388c-812d-41d2-abad-f664d8a4fc9f
bereket
bereketOP6mo ago
aac0388c-812d-41d2-abad-f664d8a4fc9f
Brody
Brody6mo ago
make sure you are listening on the $PORT environment variable
bereket
bereketOP6mo 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}`);
});
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}`);
});
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
seem to be getting this now, weird
Brody
Brody6mo ago
there is no HOST variable, just use 0.0.0.0
bereket
bereketOP6mo ago
I have it set to (where these are under the env cost)
HOST: z.string().default("0.0.0.0"),
API_URL: z.string().default("http://localhost:8080"),
HOST: z.string().default("0.0.0.0"),
API_URL: z.string().default("http://localhost:8080"),
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}`);
});
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}`);
});
Brody
Brody6mo ago
what is PORT set to in zod ?
bereket
bereketOP6mo ago
PORT: z.number().default(8080),
HOST: z.string().default("0.0.0.0"),
PORT: z.number().default(8080),
HOST: z.string().default("0.0.0.0"),
Brody
Brody6mo 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
bereket
bereketOP6mo ago
will try rn but i doubt thats the issue since it should be defined
Brody
Brody6mo ago
is it? do you have a PORT service variable? you'd still want to be listening on the PORT environment variable in code though
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
running into this issue now when i try to deploy those new changes (but anything really)
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
keeps retrying
Brody
Brody6mo ago
would you happen to have a custom start command set now?
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
the structure of my codebase is:
apps
-> api
package.json
apps
-> api
package.json
Brody
Brody6mo 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
bereket
bereketOP6mo ago
nope i dont misread my bad
bereket
bereketOP6mo ago
No description
Brody
Brody6mo ago
hmmm well those logs are not helpful, bun never prints anything helpful it seems I assume you can still run the same pair of build and start commands locally without issues?
bereket
bereketOP6mo ago
yeah it all works locally same start & build commands
Brody
Brody6mo ago
even if you deleted the project and did another clone?
bereket
bereketOP6mo ago
on railway or git
Brody
Brody6mo ago
on git, delete the project locally and clone it again 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
bereket
bereketOP6mo 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": "[email protected]",
"workspaces": [
"apps/*"
],
"dependencies": {
"tsc-alias": "^1.8.10"
}
}
{
"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": "[email protected]",
"workspaces": [
"apps/*"
],
"dependencies": {
"tsc-alias": "^1.8.10"
}
}
i use [email protected]
Brody
Brody6mo ago
what version does railway use?
bereket
bereketOP6mo ago
where can i check that?
Brody
Brody6mo ago
temporarily change the start command to a command that prints the version of bun
bereket
bereketOP6mo ago
alright
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
ok
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
used the railway cli thank god i got a bit more context
Brody
Brody6mo ago
are you on the v2 runtime?
bereket
bereketOP6mo ago
yeah
Brody
Brody6mo ago
also, I the API app not with bun?
bereket
bereketOP6mo ago
wdym
Brody
Brody6mo ago
node dist/index.js
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
got it up to this point now still getting
Application failed to respond
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"
},
"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"
},
Brody
Brody6mo ago
we are back to this, please make these changes
bereket
bereketOP6mo ago
oh shoot forgot to change that back what's the port railway uses by default? just curious
Brody
Brody6mo ago
it's random, that's why your app needs to listen on the PORT environment variable please make this change
bereket
bereketOP6mo ago
doing that rn alright just tried with:
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}`);
}
);
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}`);
}
);
seems to keep using 8080 thought, so PORT must not be defined by railway
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
wtf am i doing something wrong ok so apparently it works if i just manually add PORT to my env variables thank you
Brody
Brody6mo ago
while that will work, and it's by far the simpler way this is not the recorded way
bereket
bereketOP6mo ago
okay so after a bit of debugging it seems like dotenv just doesnt want to parse the env variable on railway it works locally though, which is weird
Brody
Brody6mo ago
dotenv parses .env files
bereket
bereketOP6mo ago
yeah
Brody
Brody6mo ago
railway does not create .env files they inject the variables directly into the environment
bereket
bereketOP6mo ago
wdym how would process.env not work? i understand this but not this
Brody
Brody6mo ago
I don't know how I could clarify further
bereket
bereketOP6mo ago
well maybe i can explain what i mean
bereket
bereketOP6mo ago
i have variables that i set on railway, like below:
No description
bereket
bereketOP6mo ago
and im accessing them through process.env but when i test it by console logging, like console.log(process.env.GOOGLE_CLIENT_ID ?? "some google client id"), it doesn't work, and goes to the fallback of some google client id
Brody
Brody6mo ago
your code is doing something to unset them
bereket
bereketOP6mo ago
oh actually i think the issue is that the env variables are located in the root directory and not in apps/api
Brody
Brody6mo ago
the environment variables are injected into the containers environment, they aren't scoped to any specific directory in a production environment like railway you don't even need to call dotenv
bereket
bereketOP6mo 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);
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);
bereket
bereketOP6mo ago
No description
bereket
bereketOP6mo ago
I have it set in Railway and in my src/index.ts, all I do is console.log(env.DATABASE_URL) and for some reason it doesn't return the value that I put in railway and instead that fallback of default db url goes here
Brody
Brody6mo ago
can zod even parse process.env
bereket
bereketOP6mo ago
yes
Brody
Brody6mo ago
haha don't say yes, if it could, it would work
bereket
bereketOP6mo ago
have done it before + works locally
Brody
Brody6mo ago
so you have something missconfigured
bereket
bereketOP6mo ago
will try without using zod for it
import "dotenv/config";

export const env = {
DATABASE_URL: process.env.DATABASE_URL || "postgres://butler:[email protected]:5432/postgres",
// ... other variables
};
import "dotenv/config";

export const env = {
DATABASE_URL: process.env.DATABASE_URL || "postgres://butler:[email protected]:5432/postgres",
// ... other variables
};
doesn't seem to work with this either the thing is it works fine locally so i can't repro it
Solution
Brody
Brody6mo ago
something you are doing is un-setting them
Brody
Brody6mo ago
try removing dotenv because as mentioned, you don't need to call it when on railway
Want results from more Discord servers?
Add your server