2 years ago
I encountered an issue while trying to deploy my MERN stack application on Railway. The build process failed during Docker build, specifically when attempting to build the frontend part of the application. The error message suggests a problem with locating the frontend package.json file.
I have a mono repository structure for my app, with the backend located in the root directory and the frontend in a separate directory named client. I've tried to configure the build process in the backend's package.json to handle building both the frontend and backend together, but it seems Railway's environment does not support changing directories (cd ..).
I need assistance in resolving this issue and successfully deploying my MERN stack app on Railway. The goal is to create a Docker container that includes both the frontend and backend of the application.
Any guidance on how to properly configure the build process and Dockerfile for a MERN stack app on Railway would be greatly appreciated. Thank you!
This is the file structure:
root/
│
├── frontend/
│ └── package.json
│
└── backend/
└── package.json
These are the build commands:
"build": "npm run build-client && npm run build-server",
"build-client": "npm --prefix ../frontend install ../frontend && npm --prefix ../frontend run build && cd -",
"build-server": "npm run build"
ⓘ Deployment information is only viewable by Project members and Railway admins.
52 Replies
2 years ago
You have two apps, a frontend and a backend, meaning you will need two Railway services, one for the frontend and one for the backend.
I've tried to configure the build process in the backend's package.json to handle building both the frontend and backend together
From my experience having your backend do that just complicates things significantly.
it seems Railway's environment does not support changing directories (cd ..)
It does, but it should never be done since there are always better ways, it also creates an extraordinarily fragile build system.
Please share your repository so that I can give specific help.
2 years ago
Did you have an AI write that railway.toml file? its not even remotely close to being valid syntax.
2 years ago
yeah, i don't know how to write one
2 years ago
Neither does the AI haha.
Give me a few minutes to look that repo over and come up with a plan.
2 years ago
cool
2 years ago
Some code changes are going to be needed for an optimal deployment.
Removes lines 17, 35, 37, 39-41 from your app.js file in the backend.
2 years ago
basically the stuff which tells backend to serve files from dist, right?
Attachments
2 years ago
Looks like you've been trying for quite some time now, well get you deployed!
basically the stuff which tells backend to serve files from dist, right?
exactly!
2 years ago
While you make these code changes, you should remove the repo from the service from within the service settings.
2 years ago
Correct, we'll connect it back later, but for now we don't need Railway trying to deploy every little code change you make until we are ready.
2 years ago
Theres going to be a few more code changes, so let me know when you have done the removal of those lines, and we can move on to the next steps!
2 years ago
pushed just now
2 years ago
Remove your build script stuff from the backend's package.json, none of it will be applicable going forward.
And if you have any build or start commands set in your Railway service settings, remove them.
2 years ago
done
2 years ago
For simplicity's sake, can you rename your current service to just Backend and attach a new screenshot of your project after that, please?
2 years ago
Yep!
Can I assume you already have your service variables set up? like MONGODB_URI
2 years ago
yes all of them
2 years ago
Okay I think the backend is in order, go ahead and connect the repo to the service and make sure you set the root directory to /backend
2 years ago
yeah, its live
2 years ago
how to share logs?
2 years ago
https://github.com/brody192/vite-react-template
Copy the nixpacks.toml and Caddyfile from this repo into your frontend folder.
2 years ago
done
2 years ago
Add a new empty service to the Railway project, and name it Frontend
Make sure to generate a domain for it.
2 years ago
root dir will be frontend, right?
2 years ago
cors issue, i will need to change the cors from localhost to frontend domain right?
2 years ago
its working
2 years ago
i think i will have to include credentials now with each request where it is required
2 years ago
earlier i had it on render where both frontend and backend were like a single service so i did not need to do it
2 years ago
i see… i also learned a different way of serving content over the web where both frontend and backend are on different domains
2 years ago
so thank you!
2 years ago
Fun fact that's how Railway does it too, the dashboard and blackboard are on different domains!
2 years ago
i mean, i knew that big companies have their backend and frontend on different domains but could never do it. When i deployed my first application, i had the frontend on vercel and backend on render but could not achieve the desired result. Then i switched to using just render. I was not satisfied with it so now thought of trying this.
2 years ago
I am overwhelmed with joy from the support i received.