MCPcopy Index your code
hub / github.com/nodejs/node / spawn

Function spawn

lib/child_process.js:789–835  ·  view source on GitHub ↗

* Spawns a new process using the given `file`. * @param {string} file * @param {string[]} [args] * @param {{ * cwd?: string | URL; * env?: Record ; * argv0?: string; * stdio?: Array | string; * detached?: boolean; * uid?: number; * gid?: number; * serializat

(file, args, options)

Source from the content-addressed store, hash-verified

787 * @returns {ChildProcess}
788 */
789function spawn(file, args, options) {
790 options = normalizeSpawnArguments(file, args, options);
791 validateTimeout(options.timeout);
792 validateAbortSignal(options.signal, 'options.signal');
793 const killSignal = sanitizeKillSignal(options.killSignal);
794 const child = new ChildProcess();
795
796 debug('spawn', options);
797 child.spawn(options);
798
799 if (options.timeout > 0) {
800 let timeoutId = setTimeout(() => {
801 if (timeoutId) {
802 try {
803 child.kill(killSignal);
804 } catch (err) {
805 child.emit('error', err);
806 }
807 timeoutId = null;
808 }
809 }, options.timeout);
810
811 child.once('exit', () => {
812 if (timeoutId) {
813 clearTimeout(timeoutId);
814 timeoutId = null;
815 }
816 });
817 }
818
819 if (options.signal) {
820 const signal = options.signal;
821 if (signal.aborted) {
822 process.nextTick(onAbortListener);
823 } else {
824 addAbortListener ??= require('internal/events/abort_listener').addAbortListener;
825 const disposable = addAbortListener(signal, onAbortListener);
826 child.once('exit', disposable[SymbolDispose]);
827 }
828
829 function onAbortListener() {
830 abortChildProcess(child, killSignal, options.signal.reason);
831 }
832 }
833
834 return child;
835}
836
837/**
838 * Spawns a new process synchronously using the given `file`.

Callers 15

launchTargetFunction · 0.90
forkFunction · 0.70
execFileFunction · 0.70
launchChildProcessFunction · 0.50
runTestFileFunction · 0.50
startFunction · 0.50
compare.jsFile · 0.50
runBenchmarkFunction · 0.50
goFunction · 0.50
mainFunction · 0.50

Calls 12

normalizeSpawnArgumentsFunction · 0.85
validateAbortSignalFunction · 0.85
sanitizeKillSignalFunction · 0.85
killMethod · 0.80
validateTimeoutFunction · 0.70
setTimeoutFunction · 0.70
clearTimeoutFunction · 0.70
debugFunction · 0.50
requireFunction · 0.50
addAbortListenerFunction · 0.50
emitMethod · 0.45
onceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…