R
Railway10mo ago
Liam

Running a Bun + pnpm Application on Railway

Hi! I have a monorepo where I use pnpm + turborepo for repo & package managment. One of the apps in the repository, a discord bot, uses bun as its runtime, but still uses pnpm for the actual package installation. Any idea how I can go about getting this working w/ nixpacks / railway? Thanks!
Solution:
or just use 1.0.23 on railway, add nixpkgsArchive = 'e99651a836b162b48fbf4195e0c8d3028b6b32a8' below your [phases.setup]
Jump to solution
54 Replies
Percy
Percy10mo ago
Project ID: bc172a5f-caaa-4872-a59d-62201654b4f6
Liam
LiamOP10mo ago
bc172a5f-caaa-4872-a59d-62201654b4f6
Brody
Brody10mo ago
I'd probably go about writing a custom nixpacks plan for that specific app where I would have a file like bot.nixpacks.toml with all the applicable configurations https://nixpacks.com/docs/configuration/file and then the NIXPACKS_CONFIG_FILE service variable set to name of my nixpacks file so that the custom build plan is only used for the bot services
Liam
LiamOP10mo ago
Got it, so would I want the nixpacks file to be at the root of the repo?
Brody
Brody10mo ago
correct
Liam
LiamOP10mo ago
Also, I am new to nixpacks config stuff. Does this seem like a good starting point?
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install']

[start]
cmd = 'pnpm run start'
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install']

[start]
cmd = 'pnpm run start'
Not really sure how my custom root for the service comes into play here
Liam
LiamOP10mo ago
No description
Brody
Brody10mo ago
does any of the code in that folder access code outside of that folder? aka a shared monorepo
Liam
LiamOP10mo ago
Yes, it accesses some shared packages in the /packages/ directory
Brody
Brody10mo ago
yeah looks decent, replace with the applicable bun build and start commands, also add an install phase with the applicable bun command then setting the root directory to what you have would only copy in what's in that set directory meaning your bot code can't import something from directories elsewhere, it would need to be set to the root directory and that's where your custom bot.nixpacks.toml would live
Liam
LiamOP10mo ago
Got it got it, have went ahead and done that, running into the following error during build:
No description
Brody
Brody10mo ago
I thought you wanted to use bun for the install, build, and start commands, looks like you're still using pnpm there. for your install, build, and start commands in the custom build plan you would need to use commands that run in a set directory, so that for example your install command only installs the packages that the bot app requires
Liam
LiamOP10mo ago
Yep, sorry for the miscommunication! I am only using bun for a runtime. Currently I now have the service root set as just the root of the monorepo. Are you saying that pnpm install in the root would not work? On my local machine, pnpm handles monorepo stuffs so running pnpm install at root installs for all packages & apps.
Brody
Brody10mo ago
okay only bun for runtime but pnpm for package management, got it! yes pnpm install ran in the root will install all packages for all apps, but we only want to install the packages the bot needs, pnpm provides an applicable flag or argument of some sort to do such a thing but I don't know it off the top of my head
Liam
LiamOP10mo ago
I can't find the command for chaining multiple packages / apps together in one command, but giving this a shot
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'pnpm run start --filter bot'
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'pnpm run start --filter bot'
Brody
Brody10mo ago
why are you trying to run multiple installs, this is only the bot service unless those are needed by the bot service?
Liam
LiamOP10mo ago
The others are for dependent local packages in the monorepo, bot depends on config and db and I don't belive it cascades the install.
Brody
Brody10mo ago
noted
Liam
LiamOP10mo ago
Yep 👍
Brody
Brody10mo ago
then that looks like it would do the trick, sorry I'm having to do a lot of guess work here since I don't have much knowledge of shared monorepos and pnpm
Liam
LiamOP10mo ago
Its all good! Very much apreciate the help :)
Liam
LiamOP10mo ago
Still seems to be having trouble installing pnpm v 8.3.1? I found this thread and have updated my package.json to include the pnpm version but I think the issue is that npm 404s when trying to install it. https://www.answeroverflow.com/m/1131260702330462219
build fails with "ERR_PNPM_LOCKFILE_CONFIG_MISMATCH" - Railway
Pretty much what the title says. Following the Nixpacks command order works both on my computer and a friend's (pnpm i --frozen-lockfile -> pnpm build -> pnpm start) Full error: ``` #10 [stage-0 6/10] RUN --mount=type=cache,id=s/e15ed82c-c15e-4b05-b9dc-06a185373d18-/root/local/share/pnpm/store/v3,target=/root/.local/share/pnpm/store/v3 pnpm i ...
No description
Brody
Brody10mo ago
is npm having issues right now or something?
Liam
LiamOP10mo ago
Dosen't look like it :/ https://status.npmjs.org/ Super odd issue, cause my understanding is that once a package is depended on you can't remove it right? So no real reason it should 404...
npm Status
Welcome to npm's home for real-time and historical data on system performance.
Brody
Brody10mo ago
wait, that log screenshot says pnpm 8.31.1 but the latest version is 8.15.2?? https://github.com/pnpm/pnpm/releases unless im missing something here?
Liam
LiamOP10mo ago
no yeah that is really bizzare
Brody
Brody10mo ago
do you have 8.31.1 set somewhere? and what is your local pnpm version?
Liam
LiamOP10mo ago
oh
No description
Liam
LiamOP10mo ago
I have it set as 8.3.1 in the app, but that at the root. Let me fix that and see if it works.
Liam
LiamOP10mo ago
Looking good 👀
No description
Brody
Brody10mo ago
oh thats much progress!
Liam
LiamOP10mo ago
Builds now working! Odd issue though where it just lists the scripts in the package.json instead of running the actual bot though...
No description
Brody
Brody10mo ago
what's with the mention of .env files?
Liam
LiamOP10mo ago
Sort of a bun replacement for libraries like dotenv. That part is only used in dev, and it does not lead to any errors if the file does not exist and does not overwrite existing enviroment variables.
Brody
Brody10mo ago
okay cool, then that's very odd for sure hey where you able to get the last leg of this issue sorted out?
Liam
LiamOP10mo ago
Sadly I still haven't, have tried a few things but to no avail :/
Brody
Brody10mo ago
it's always just printing out the available scripts?
Liam
LiamOP10mo ago
Yep, only difference between my local environment and the railway one as far as I can tell is the version of bun (1.0.11 on railway vs 1.0.23 on local)
Brody
Brody10mo ago
does bun even have an actual command to print available scripts?
Liam
LiamOP10mo ago
Only think I have found is this which seems like the behaviour changed at some point https://github.com/oven-sh/bun/discussions/7585
GitHub
Script to output available scripts like npm run · oven-sh bun · D...
I frequently used the old behavior of bun run with no arguments to see the available scripts in a project. Now that it includes the full help and examples every time it's not so useful. Here is...
Liam
LiamOP10mo ago
Almost seems like bun run is not getting any arguments? That's really odd though as it definently is getting some.
Brody
Brody10mo ago
show me again where and how you set your start command?
Liam
LiamOP10mo ago
I do it via this nixpacks toml:
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'pnpm run start --filter bot'
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'pnpm run start --filter bot'
with the bot app package.json having the following scripts:
"scripts": {
"dev": "bun run --env-file=../../.env --hot bot.ts",
"start": "bun run --env-file=../../.env ./bot.ts",
"deploy:dev": "bun run --env-file=../../.env --dev deploy-commands.ts",
"deploy:prod": "bun run --env-file=../../.env --prod deploy-commands.ts"
}
"scripts": {
"dev": "bun run --env-file=../../.env --hot bot.ts",
"start": "bun run --env-file=../../.env ./bot.ts",
"deploy:dev": "bun run --env-file=../../.env --dev deploy-commands.ts",
"deploy:prod": "bun run --env-file=../../.env --prod deploy-commands.ts"
}
running pnpm run start --filter bot from the root of the monorepo on my local machine leads to the bot starting correctly
Brody
Brody10mo ago
what does the build table say the start command is?
Liam
LiamOP10mo ago
Where would the build table be at? This is what shows in settings for the service.
No description
Brody
Brody10mo ago
in the build logs
Liam
LiamOP10mo ago
ah got it thx
No description
Brody
Brody10mo ago
thats not quite the command you showed me above?
Liam
LiamOP10mo ago
sorry my bad had not copied the most recent version of the nixpacks toml
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'bun -v && pwd && pnpm --filter=bot run start'
[phases.setup]
nixPkgs = ['...', 'bun']

[phases.build]
cmds = ['pnpm install --filter db', 'pnpm install --filter config', 'pnpm install --filter bot']

[start]
cmd = 'bun -v && pwd && pnpm --filter=bot run start'
Liam
LiamOP10mo ago
this is the deploy output
No description
Brody
Brody10mo ago
can you reproduce this behavior locally if you use bun 1.0.11?
Solution
Brody
Brody10mo ago
or just use 1.0.23 on railway, add nixpkgsArchive = 'e99651a836b162b48fbf4195e0c8d3028b6b32a8' below your [phases.setup]
Liam
LiamOP10mo ago
Setting the bun version worked! Thank you!
Brody
Brody10mo ago
Awesome, through I'm still curious, can you reproduce that behaviour with bun 1.0.11 locally?
Want results from more Discord servers?
Add your server