Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baumgars's avatar

exec() does not wait for await fetch() in node script

Hi,

is there a reason the script stops executing? The node script runs until the end via normal command line, it runs until the end via tinker but when i run it through my laravel backend...i put several console.log() inside the script so i can see that after calling fetch() the execution of the script stops and returns all i logged until that point.

0 likes
3 replies
LaryAI's avatar

Certainly! This issue often occurs when running Node.js scripts from a PHP backend (like Laravel) using exec() or similar functions. The root cause is usually related to how PHP handles child processes and their standard input/output streams, especially when the Node.js script performs asynchronous operations (like await fetch()).

Why does this happen?

  • When you run a Node.js script with asynchronous code (such as await fetch()), the process may not flush its output or may be terminated before the asynchronous operation completes, especially if the parent PHP process closes the pipes or times out.
  • PHP's exec() (and similar functions like shell_exec() or proc_open()) can sometimes close the process before Node.js finishes its async work, especially if the script doesn't explicitly wait for all promises to resolve.

How to fix it:

  1. Ensure your Node.js script is properly await-ing all asynchronous operations and not exiting early.
  2. Flush all output before exiting.
  3. Use proc_open() in PHP for better process control if needed.
  4. Try running your Node.js script with the --unhandled-rejections=strict flag to catch unhandled promise rejections.

Example Node.js Script:

// script.js
const fetch = require('node-fetch');

async function main() {
    console.log('Before fetch');
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
        const data = await response.json();
        console.log('Fetched data:', data);
    } catch (e) {
        console.error('Fetch error:', e);
    }
    console.log('After fetch');
}

main().then(() => {
    // Ensure all logs are flushed before exit
    process.exit(0);
});

Example PHP Code:

$output = [];
$return_var = 0;
exec('node /path/to/script.js 2>&1', $output, $return_var);

echo implode("\n", $output);

Tips:

  • Make sure your Node.js script does not exit before all async code is done.
  • If you still have issues, try using proc_open() in PHP for more control over the process.
  • Consider logging errors in your Node.js script to a file to see if there are unhandled exceptions.

Summary:
The problem is likely due to the PHP process not waiting for the Node.js script's async operations to finish. Ensure your Node.js script is correctly awaiting all async code, and consider using proc_open() for more robust process management from PHP.

Let me know if you need a more specific example or further troubleshooting!

baumgars's avatar

Can i somehow block these useless AI responses?

baumgars's avatar

I used Process run with a log in the closure to figure out the node infrastructure crashed. exec() and shell_exec() do not output such things.

Please or to participate in this conversation.