NodeJS and Python Serverless Funciton in same project

Hello,

I have a Nodejs project which contains some serverless functions within api/ and its working fine. I also have a Python script that I would like to include in my project as a python serverless funciton. I’ve tried a few things and they didn’t work so, I went through a few other help threads and have tried their suggestions but so far the suggestions haven’t worked for me.

I can explain what I’ve tried and show the settings for my files but before doing that I want to know if this is true:

Within one of the help threads: Python script within nodeJS app - #2 by anshumanb Anshuman Bhardwaj mentions the following:

Is it really impossible to be able to have a Python serverless function alongside NodeJs serverless functions within /api? Can’t we just configure which runtime to use for a specific serverless function? My project isn’t using any framework, just Typescript and Python.

If there is a way to have Python and NodeJs serverless functions within the same project please let me know so I can share more details.

Hi @nazcodeland, thanks for asking this question.

To clarify, you can keep Python and Node.js serverless functions in the same project. The question that you linked was about using Python script inside a Node.js function, which is not supported because each function has a dedicated runtime.

This is a project that I’ve which has both Python and Node.js functions

I hope this answers your question.

Thank you for the response and I apologize for misreading the other help thread.

I deployed a project that contains Node & Python serverless functions to Vercel and both endpoints work. However, when developing locally using the Vercel CLI I keep getting, this huge error:

when accessing http://localhost:8071/api/node

 vercel dev --listen 8080
Vercel CLI 41.2.2
> Ready! Available at http://localhost:8080
Server is running on http://localhost:3000
file:///C:/Users/o0/.bun/install/global/node_modules/@vercel/node/dist/dev-server.mjs:1133
    return listener(req, res);
           ^
TypeError: listener is not a function
    at Server.<anonymous> (file:///C:/Users/o0/.bun/install/global/node_modules/@vercel/node/dist/dev-server.mjs:1133:12)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: Command failed: taskkill /pid 1808 /T /F
ERROR: The process "1808" not found.

    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:517:28)
    at ChildProcess.emit (node:domain:489:12)
    at maybeClose (node:internal/child_process:1098:16)
    at ChildProcess._handle.onexit (node:internal/child_process:303:5)
Error: An unexpected error occurred!
Error: Command failed: taskkill /pid 1808 /T /F
ERROR: The process "1808" not found.

    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:517:28)
    at ChildProcess.emit (node:domain:489:12)
    at maybeClose (node:internal/child_process:1098:16)
    at ChildProcess._handle.onexit (node:internal/child_process:303:5)

and when accessing http://localhost:8071/api/python

 vercel dev --listen 8071
Vercel CLI 41.2.2
> Ready! Available at http://localhost:8071
> Building @vercel/python@latest:api/main_flow.py
Installing required dependencies...
> Built @vercel/python@latest:api/main_flow.py [3s]
AssignProcessToJobObject: (87) The parameter is incorrect.
LambdaError: RequestId: a3e98b22-7754-4aa4-aa3b-c7966447871f Process exited before completing request
    at Lambda.<anonymous> (C:\Users\o0\.bun\install\global\node_modules\@vercel\fun\dist\src\index.js:114:27)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\o0\.bun\install\global\node_modules\@vercel\fun\dist\src\index.js:28:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
using HTTP Handler
Traceback (most recent call last):
  File "C:\Python311\Lib\urllib\request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Python311\Lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1037, in _send_output
    self.send(msg)
  File "C:\Python311\Lib\http\client.py", line 975, in send
    self.connect()
  File "C:\Python311\Lib\http\client.py", line 941, in connect
    self.sock = self._create_connection(
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\socket.py", line 850, in create_connection
    raise exceptions[0]
  File "C:\Python311\Lib\socket.py", line 835, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\o0\AppData\Local\com.vercel.fun\Cache\runtimes\python\bootstrap.py", line 147, in <module>
    lambda_runtime_main()
  File "C:\Users\o0\AppData\Local\com.vercel.fun\Cache\runtimes\python\bootstrap.py", line 130, in lambda_runtime_main
    (event, context) = lambda_runtime_next_invocation()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\o0\AppData\Local\com.vercel.fun\Cache\runtimes\python\bootstrap.py", line 56, in lambda_runtime_next_invocation
    res = LambdaRequest('invocation/next')
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\o0\AppData\Local\com.vercel.fun\Cache\runtimes\python\bootstrap.py", line 31, in __init__
    req = urllib.request.urlopen(url, data)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 1377, in http_open
    return self.do_open(http.client.HTTPConnection, req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] No connection could be made because the target machine actively refused it>

I changed up the ports because I thought it might have to do with ports being in use. Long errors, I just wanted to share but maybe I’m just tired since its late here, I’ll take a look at these tomorrow and update.

Note: I installed the Vercel CLI using Bun, I don’t know if that might be the issue.

1 Like

Hi @nazcodeland, are you able to start these individual files using node and python directly?

If you need more help, please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

Hey, I managed to make some progress but I’m still doing something wrong. This repository: GitHub - NazCodeland/vite-vercel works when I push it to Vercel.

I can access the api/node and api/python but when I am running it locally, I can only access api/node and when I try to access api/python I get the following error:

404: NOT_FOUND
Code: NOT_FOUND
ID: dev1::m0dro-1740772524199-12ed8c11ac38

Hmm, I just tried deploying the hello-world Python template and that is also erroring when I run it locally but works when hosted on Vercel, accesed via *.vercel.app url.

But the error is different, when I go to http://localhost:3000/api it shows

502: BAD_GATEWAY
Code: NO_RESPONSE_FROM_FUNCTION
ID: dev1::dev1::11jq7-1740773958622-87294de9dd2d


Check the logs in your terminal window to see the application error.

in the terminal:

vercel dev
Vercel CLI 41.2.2
? Set up and develop “~\source\developer\projects\mine\vanilla\python-hello-world”? yes
? Which scope should contain your project? nazcodeland's projects
? Found project “nazcodelands-projects-bac6b4e1/python-hello-world”. Link to it? yes
🔗  Linked to nazcodelands-projects-bac6b4e1/python-hello-world (created .vercel and added it to .gitignore)
> Ready! Available at http://localhost:3000
> Building @vercel/python@latest:api/index.py
Installing required dependencies...
> Built @vercel/python@latest:api/index.py [5s]
AssignProcessToJobObject: (87) The parameter is incorrect.
LambdaError: RequestId: e2793808-9871-48fe-81b5-62fae5e3df19 Process exited before completing request
    at Lambda.<anonymous> (C:\Users\o0\.bun\install\global\node_modules\@vercel\fun\dist\src\index.js:114:27)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\o0\.bun\install\global\node_modules\@vercel\fun\dist\src\index.js:28:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
using HTTP Handler
Traceback (most recent call last):
  File "C:\Python311\Lib\urllib\request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Python311\Lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Python311\Lib\http\client.py", line 1037, in _send_output
    self.send(msg)
  File "C:\Python311\Lib\http\client.py", line 975, in send
    self.connect()
  File "C:\Python311\Lib\http\client.py", line 941, in connect
    self.sock = self._create_connection(
                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\socket.py", line 850, in create_connection
    raise exceptions[0]
  File "C:\Python311\Lib\socket.py", line 835, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\o0\AppData\Local\com.vercel.fun\Cache\runtimes\python\bootstrap.py", line 147, in <module>
    lambda_runtime_main()

at least with this it tried to build and install dependencies,

> Building @vercel/python@latest:api/index.py
Installing required dependencies...
> Built @vercel/python@latest:api/index.py [5s]
...

whereas in the template I made using vercel init GitHub - NazCodeland/vite-vercel it doesn’t even try to build/install when I try to access the api/python locally.