Next.js Discord

Discord Forum

Deploying Next.js app with both Node and Python serverless functions

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
I am trying to add a python endpoint to my existing next.js app. I followed this example: https://github.com/vercel/examples/tree/main/python/nextjs-flask

However, the build for my preview environment on Vercel does not show the python function in the deployment summary. Is there anything I'm missing or how should I try debugging this situation?
Answered by Ray
you have to move the api folder to the root
View full answer

26 Replies

@Cape lion I am trying to add a python endpoint to my existing next.js app. I followed this example: https://github.com/vercel/examples/tree/main/python/nextjs-flask However, the build for my preview environment on Vercel does not show the python function in the deployment summary. Is there anything I'm missing or how should I try debugging this situation?
I got 500 error too after deploying the template
the error is
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': cannot import name 'url_quote' from 'werkzeug.urls' (/var/task/werkzeug/urls.py)
Traceback (most recent call last):
Cape lionOP
hmm my requirements txt has
Flask==3.0.2
...
Werkzeug==3.0.1
Cape lionOP
yea, i'm thinking this is something to do with Vercel and not Next (my mistake)
numpy==1.24.3
Flask==3.0.0

just tested with these and it works
let me try 3.0.2
Cape lionOP
running vercel build list the app routes at the end and the python route does not show up in that list
@Ray let me try 3.0.2
this work too
@Cape lion running `vercel build` list the app routes at the end and the python route does not show up in that list
did you mean it doesn't show from here?
Cape lionOP
yes and the output directory as well .vercel/output/functions...
but can you access /api/python?
Cape lionOP
in local i can, but in my generated vercel preview environment it's a 404
I can see it
.vercel/output/functions/api/index.func/api/index.py
Cape lionOP
hmm I definitely missed something then -- I tried to add the flask server to my existing next app rather than starting from the template
Cape lionOP
unfortunately, it's a private repo. I did spot a difference though.
the template has index.py under /api folder, but my repo has it as /api/some-path/index.py
oh also, my next app is using app router so the path is actually /app/api/some-path/index.py
you have to move the api folder to the root
Answer
Cape lionOP
that did the trick, thank you!