My Node JS API, which uses a Child process, works when run by nodemon, cmd but in PM2 generates error

Asked

Viewed 22 times

0

My API runs a python script through an Express route, using Child process, I have tried to run only the script using python nameScript and it worked, I do not know why is giving this Python ENOENT error. Part of the code in Node JS:

    app.post('/python', chamandoPython)

    async function chamandoPython(req, res) {
      var process = spawn('python', ["./PythonServices/main.py", 
       "./uploads/"]).on('error', function( err ){ throw err })
      process.stdout.on('data', async function (data) {
        await res.send(data.toString())
      })
      process.stderr.on('data', async function (data) {
        console.log("stderr" + data.toString())
      })
     }

When I run this file by nodemon, it works perfectly, but when "starto" it by PM2, returns this error:

    $Error: spawn python ENOENT
    $  at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
    $  at onErrorNT (internal/child_process.js:470:16)
    $  at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    $errno: 'ENOENT',
    $code: 'ENOENT',
    $syscall: 'spawn python',
    $path: 'python',
    $spawnargs: [ './PythonServices/main.py', './uploads/' ]
    $}

I am trying to put my API on a server, the code on my machine works for both nodemon/Node and PM2 but on the server, it only works when it runs on nodemon/Node. The same code generates this error only when running on PM2.

  • Usually this error is related to when the file cannot be found. The only thing that can explain this behavior is file execution/visualization permissions, in the filesystem, could give a search via ls-la to check permissions vs pm2 and Node execution.

  • Thanks for the answer, I will try to know more about the filesystem and about this ls, if you have any document that can pass me I will be even more grateful. Thanks again!

  • This "./PythonServices/main.py" is using relative path. Use "/caminho/completo/para/seu/script/PythonServices/main.py". Where /caminho/completo/para/seu/script/ are the directories from the root to the script. Got confused, but I hope you understand.

  • The error is not related to the directory, the python that is not being recognized when I run the API by pm2. I have tested with relative and complete path and continued with the same error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.