Calling Claude CLI as a child process yields no output
06:19 21 Nov 2025

I am trying to use the automation features built into the Claude CLI. Namely, if you pass in the --print flag argument, with a prompt (and --allowed-tools to make it actually do something) it will just make the changes and exit, not showing the usual interface. This works well using it in the terminal (Mac OS). But when I try to call it using a node.js child process, it doesn't seem to yield any output. Here's what I'm doing:

import {spawn} from 'child_process';


//var c = spawn('./hello',['Pepe'],{stdio:'pipe'});
var c = spawn('claude',['--print'],{stdio:'pipe'});
c.on('error', (err)=> console.log("Error ejecutando comando:" + err));
c.stdout.on('data',(chunk)=> console.log(chunk.toString('utf8')));
c.stderr.on('data',(chunk)=>console.log(chunk.toString('utf8')));

c.stdin.write('Create a file called hello.js and make a function that displays hello   world on the console');

Note that I am passing the --print flag alone, this should make the process fail with an error, but no output is reported. The "hello" process commented above is a C program that simply outputs Hello World. This one works and accepts the parameter passed in the spawn() call. In that one, the ouput is correctly piped into the node process and output to the console. So the question is, why doesn't it work with the Claude CLI?

node.js child-process claude-code