How to fix 'Application failed to respond' error and add Python (flask) to Nextjs app on Railway?
Context/What I want to do:
I have an app that uses nextjs and flask. I have the following folder structure:
and on start I want to run server.py and also npm start.
What I'm doing
Locally, I run
python server.py
to start the flask server and npm run build
to build the static nextjs files for my app. On Railway, I entered python server.py && yarn start
as the custom start command but then I get the "Application failed to respond" error on my page. When I remove the custom start command, Railway only runs yarn start and my page works again, but the Python flask server is not being started.
From the docs (https://docs.railway.app/guides/fixing-common-errors) it looks like Railway is unable to connect to my app, but the only thing I changed is to alter the start command to python server.py && yarn start
. The docs suggest to open the app on port 3000 which I tried with the command in the "What I tried" section.
What I tried:
- Changing the custom start command to python server.py && next start --port ${PORT-3000}
as specified in the docs above
- Using npm instead of yarn
- Changing the folder structure (with the nextjs and the flask app separeted) - that didn't change anything, and Railway then didn't autodetect the languages and so running npm for example didn't work since npm was not available
- Setting the custom build command to npm run --prefix .\client\ build
and the custom start command to python server/server.py && npm run --prefix .\client\ start
How can I fix this issue?Solution:Jump to solution
add another empty service to the project
name it accordingly
generate a domain
set it's root directory
connect your repo...
38 Replies
Project ID:
07b4ef59-bc5e-4049-bf5e-294c192239dd
Service ID: 07b4ef59-bc5e-4049-bf5e-294c192239dd
you want an isolated monorepo structure, where your two apps are in separate folders, please work towards making that happen and don't worry about any failed builds yet.
when you're done please share your repo so I can make sure you've restructured your project correctly
once the structure side of things is done, we can then move on to the railway side of things
I've done that, now the nextjs app and the flask server are in separete folders (client and server, respectively)
The repo is private and I would like to keep it private if that's possible, could I add you as a collaborator separately? (if yes, feel free to DM me your GitHub so i can add you)
brody192
Thanks! Added you
looking into it, I might be heading out soon so I don't know how far into this I will be able to get
That's ok, thank you for your help!
structure looks good at first glance, can you send me a screenshot of your railway project?
sure!
so just.. this?
yep
okay so that service you currently have will be the backend going forward, so give it an applicable name
done
in its settings, set the root directory to
/server
done
add a service variable
PORT
set to 8080
done
let me know if the build fails
All works fine
I added back the custom start command
python server.py
alright so all good with the backend then?
yup
Solution
add another empty service to the project
name it accordingly
generate a domain
set it's root directory
connect your repo
ok, I did that and now the frontend fails to build with
(everything above that log chunk seems to work fine)
I tried running
yarn
based on your reply here (https://www.answeroverflow.com/m/1108809836222763200) and pushing the new yarn.lock but the error still occursTrying to deploy Next js 13 app and its failing? - Railway
Hey guys, whats the process to deploy a simple Next js app? Version 13.
Right now its failing. Is there specific steps? I'm just trying to deploy it from the github repo.
full logs
what node version do you use locally
v18.14.0
set your
engines.node
to 18
in package.json?
I did set it in package.json, but the error message persists
are you able to build it locally?
yes
is there anything useful online about that error?
not really, at least nothing that would explain why it builds locally
I now tried reverting to the commit where Railway did build and it still throws that error
I even created a new project entirely from the old commit and it still fails with that error
where is next Collecting page data from?
mainly from the api routes, which seem to be the issue here
I reverted back to a much older commit which didn't have those and now it seems to work
I will backtrack to see exactly where the issue was but from here everything should work
Thank you so much for your help!! I really appreciate it!
whatever you do, make sure you keep the isolated monorepo structure, but I'm sure you know that.
if you haven't already, please feel free to remove me from your repo!