Bun on Railway for a Remix V2 app
Hi and welcome, this article will explain how to deploy on Railway using Bun.
To demonstrate how-to, I will use a dummy Remix app in version 2.
Have a good read! (You can also follow me on Twitter 😇)
❕ Disclaimer: I try to will be short but concise on what doesn’t concern the deployment. You can skip the “Pre-Deployment” phase if you want.
Pre-Deployment
1. Install Bun
❗️ Before running that command, note that curl and unzip are required.
# curl and unzip are required so you can "sudo apt install unzip curl"
# (or whatever command your system requires)
curl -fsSL https://bun.sh/install | bash
Once it has been installed, the script tells us to reload the .bashrc and run the help.
I’m simply going to run “bun -v” and it says 1.0.0, let’s go 🚀
2. Generate the project
I will run “bun create remix”.
Be aware that for other projects/needs there’s also the command “bunx” (same as npx instead of npm).
3. Start the app (dev)
Now let’s run “bun dev” to start the app in local and 😱
I got this error “Import named renderToPipeableStream not found in module react-dom/server”:
This is due to the fact that I don’t have NodeJS installed (I started from a fresh VM).
The React doc is quite clear, this is only available on NodeJS.
So I’ve installed Node (20) and then ran “bun dev” again:
We are good to go on http://localhost:3000 🥳
I’ve also slightly changed the homepage:
Deployment on Railway
1. Prerequisites
- I ran “bun run build” in local to verify that the build is succesful
- I saved the code in a Github repository
- On Railway, I started a new project using that repository
- On Railway, in the project’s settings, I generated a domain
2. The build
After creating the Railway project, the build automatically starts.
The build is successful but if you inspect the log it is possible that the bun version used by Railway is not up to date 🤨
I tweeted about this a few days ago. If you want to fix this, you need to create a file with the Nixpacks’s commit hash referencing the version you want.
As I’m writing this, Bun 1.0.2 has been released so:
- For Railway I created the file referencing the following commit
- For my local environment I ran “bun upgrade”
After push, the build is automatically launched again:
Now that we’re good with the build, let’s have a look at le deploy 😃
3. Le deploy
And OUCH again, my deployment has failed 😱
It failed for the same reason as in local, NodeJS is apparently not there:
I think it’s weird but anyway we’re here to experiment! So let’s specify to Nixpacks that we need NodeJS installed! 💪
If you have changed nixpacks’s commit hash previously then you already have the file “nixpacks.toml”. If not then create it in your root folder.
In that file we’re going to say: “during the setup phase, install the package nodejs”, here’s an example of content:
[phases.setup]
nixPkgs = ['...', 'nodejs']
❗️ Note here that the triple dot is important. It allows us to extend any “parent” configuration. More info about this on the Nixpacks’s doc.
One more example. If you have changed the Bun version like I did, then your file should be similar to this:
[phases.setup]
nixpkgsArchive = 'd68b3b0c914aea10eee7cf2e59ef44e39bc221c9'
nixPkgs = ['...', 'nodejs']
❕ A quick reminder: this hash is Nixpacks’s commit hash indicating to use Bun v1.0.2 instead of the current default version (that may not be up to date).
Now finally after this, the app was built and deployed again. The log indicated that it’s GOOD 🚀
I can now check the generated domain: remix-railway-bun-production.up.railway.app
Alright, we’re all set 🥳
Thank you for reading until here. If you have any question or review you can contact me on Twitter (@LeBugArtisan)
Have a nice day! 😇