Runing n8n from nodejs script make him crash

hi, i run nodejs script that run n8n the function body look like this:
const n8nProcess = spawn(‘C:/Program Files/nodejs/npm.cmd’, [‘run’, ‘start’], {
cwd: “c:\init\extracted/n8n”,
env: process.env,
stdio: ‘inherit’,
detached: true
});

my problem is the terminal is close and the last thing that he print is n8n start processing
it’s not happened all the time, it occurs half of the time.
i am using 0.213 version I want to avoid to upgrade version for now
I try:
set in the environment variable
NODE_OPTIONS=“–max-old-space-size=4096”

also I try run it on a stronger machine it still happen
also I create new db file and make at least one run creation of the db file
after that 2 from 11 tries failed
also I try to remove the call from the script, and make the call from batch, and still the same

I would love to hear if anyone has a recommendation

Hey @Asaf_Shay,

If you can get the error that is outputted that would be very handy, I am not sure this is an issue with n8n itself though and it is likely to be how you are running it.

it’s crash when there is output “Initializing n8n process”

npm version 8.11.0

Is it crashing then or just closing? It could be that your spawn process is killing it early. If you run n8n in one of our supported ways does that work as expected?

what is supported way ?

basically, the terminal window is closed, I don’t know if it crashes or close by your definition
I check the log and there is nothing new

Hi, would running the n8n.cmd works? I’d register it as a service so it auto run with the windows.

thanks i will check it