R
Railway11mo ago
linear

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:
/
├── .next
├── app
├── components
├── ...
├── server.py
└── ...
/
├── .next
├── app
├── components
├── ...
├── server.py
└── ...
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?
Railway Docs
Fixing Common Errors | Railway Docs
Documentation for Railway
Solution:
add another empty service to the project name it accordingly generate a domain set it's root directory connect your repo...
Jump to solution
38 Replies
Percy
Percy11mo ago
Project ID: 07b4ef59-bc5e-4049-bf5e-294c192239dd
linear
linear11mo ago
Service ID: 07b4ef59-bc5e-4049-bf5e-294c192239dd
Brody
Brody11mo ago
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
linear
linear11mo ago
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)
Brody
Brody11mo ago
brody192
linear
linear11mo ago
Thanks! Added you
Brody
Brody11mo ago
looking into it, I might be heading out soon so I don't know how far into this I will be able to get
linear
linear11mo ago
That's ok, thank you for your help!
Brody
Brody11mo ago
structure looks good at first glance, can you send me a screenshot of your railway project?
linear
linear11mo ago
sure!
linear
linear11mo ago
so just.. this?
No description
Brody
Brody11mo ago
yep okay so that service you currently have will be the backend going forward, so give it an applicable name
linear
linear11mo ago
done
Brody
Brody11mo ago
in its settings, set the root directory to /server
linear
linear11mo ago
done
Brody
Brody11mo ago
add a service variable PORT set to 8080
linear
linear11mo ago
done
Brody
Brody11mo ago
let me know if the build fails
linear
linear11mo ago
All works fine I added back the custom start command python server.py
Brody
Brody11mo ago
alright so all good with the backend then?
linear
linear11mo ago
yup
Solution
Brody
Brody11mo ago
add another empty service to the project name it accordingly generate a domain set it's root directory connect your repo
linear
linear11mo ago
ok, I did that and now the frontend fails to build with
#12 221.2

#12 221.2 > Build error occurred

#12 221.2 Error: Collecting page data for [object Object] is still timing out after 2 attempts. See more info here https://nextjs.org/docs/messages/page-data-collection-timeout

#12 221.2 at onRestart (/app/node_modules/next/dist/build/index.js:712:39)

#12 221.2 at Worker.isPageStatic (/app/node_modules/next/dist/lib/worker.js:88:40)

#12 221.2 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:899:56

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async Promise.all (index 3)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:834:17

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:773:129



#12 221.2 error Command failed with exit code 1.

#12 221.2 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

#12 ERROR: process "/bin/bash -ol pipefail -c yarn run build" did not complete successfully: exit code: 1



-----

> [stage-0 8/10] RUN --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-next/cache,target=/app/.next/cache --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-node_modules/cache,target=/app/node_modules/.cache yarn run build:

221.2 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async /app/node_modules/next/dist/build/index.js:899:56

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async Promise.all (index 3)

221.2 at async /app/node_modules/next/dist/build/index.js:834:17

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async /app/node_modules/next/dist/build/index.js:773:129

221.2 error Command failed with exit code 1.

221.2 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

-----



Dockerfile:24

-------------------

22 | # build phase

23 | COPY . /app/.

24 | >>> RUN --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-next/cache,target=/app/.next/cache --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-node_modules/cache,target=/app/node_modules/.cache yarn run build

25 |

26 |

-------------------

ERROR: failed to solve: process "/bin/bash -ol pipefail -c yarn run build" did not complete successfully: exit code: 1
#12 221.2

#12 221.2 > Build error occurred

#12 221.2 Error: Collecting page data for [object Object] is still timing out after 2 attempts. See more info here https://nextjs.org/docs/messages/page-data-collection-timeout

#12 221.2 at onRestart (/app/node_modules/next/dist/build/index.js:712:39)

#12 221.2 at Worker.isPageStatic (/app/node_modules/next/dist/lib/worker.js:88:40)

#12 221.2 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:899:56

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async Promise.all (index 3)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:834:17

#12 221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

#12 221.2 at async /app/node_modules/next/dist/build/index.js:773:129



#12 221.2 error Command failed with exit code 1.

#12 221.2 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

#12 ERROR: process "/bin/bash -ol pipefail -c yarn run build" did not complete successfully: exit code: 1



-----

> [stage-0 8/10] RUN --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-next/cache,target=/app/.next/cache --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-node_modules/cache,target=/app/node_modules/.cache yarn run build:

221.2 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async /app/node_modules/next/dist/build/index.js:899:56

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async Promise.all (index 3)

221.2 at async /app/node_modules/next/dist/build/index.js:834:17

221.2 at async Span.traceAsyncFn (/app/node_modules/next/dist/trace/trace.js:103:20)

221.2 at async /app/node_modules/next/dist/build/index.js:773:129

221.2 error Command failed with exit code 1.

221.2 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

-----



Dockerfile:24

-------------------

22 | # build phase

23 | COPY . /app/.

24 | >>> RUN --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-next/cache,target=/app/.next/cache --mount=type=cache,id=s/353bafb6-fb4c-4d7a-b504-7a5aa5b74c39-node_modules/cache,target=/app/node_modules/.cache yarn run build

25 |

26 |

-------------------

ERROR: failed to solve: process "/bin/bash -ol pipefail -c yarn run build" did not complete successfully: exit code: 1
(everything above that log chunk seems to work fine)
linear
linear11mo ago
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 occurs
Trying 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.
linear
linear11mo ago
Brody
Brody11mo ago
what node version do you use locally
linear
linear11mo ago
v18.14.0
Brody
Brody11mo ago
set your engines.node to 18
linear
linear11mo ago
in package.json?
linear
linear11mo ago
I did set it in package.json, but the error message persists
No description
Brody
Brody11mo ago
are you able to build it locally?
linear
linear11mo ago
yes
Brody
Brody11mo ago
is there anything useful online about that error?
linear
linear11mo ago
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
Brody
Brody11mo ago
where is next Collecting page data from?
linear
linear11mo ago
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!
Brody
Brody11mo ago
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!
Want results from more Discord servers?
Add your server