utilityProcess
Process: Main
modulePath
string - Path to the script that should run as entrypoint in the child process.args
string[] (optional) - List of string arguments that will be available asprocess.argv
in the child process.options
Object (optional)env
Object (optional) - Environment key-value pairs. Default isprocess.env
.execArgv
string[] (optional) - List of string arguments passed to the executable.cwd
string (optional) - Current working directory of the child process.stdio
(string[] | string) (optional) - Allows configuring the mode forstdout
andstderr
of the child process. Default isinherit
. String value can be one ofpipe
,ignore
,inherit
, for more details on these values you can refer to documentation from Node.js. Currently this option only supports configuring andstderr
to eitherpipe
,inherit
orignore
. Configuringstdin
is not supported;stdin
will always be ignored. For example, the supported values will be processed as following:pipe
: equivalent to [‘ignore’, ‘pipe’, ‘pipe’] (the default)ignore
: equivalent to ‘ignore’, ‘ignore’, ‘ignore’]
serviceName
string (optional) - Name of the process that will appear inname
property of child-process-gone event of app. Default isnode.mojom.NodeService
.allowLoadingUnsignedLibraries
boolean (optional) macOS - With this flag, the utility process will be launched via theElectron Helper (Plugin).app
helper executable on macOS, which can be codesigned withcom.apple.security.cs.disable-library-validation
andcom.apple.security.cs.allow-unsigned-executable-memory
entitlements. This will allow the utility process to load unsigned libraries. Unless you specifically need this capability, it is best to leave this disabled. Default isfalse
.
Returns
Class: UtilityProcess
UtilityProcess
is an .
child.postMessage(message, [transfer])
message
anytransfer
MessagePortMain[] (optional)
For example:
Returns boolean
Terminates the process gracefully. On POSIX, it uses SIGTERM but will ensure the process is reaped on exit. This function returns true if the kill is successful, and false otherwise.
child.pid
A Integer | undefined
representing the process identifier (PID) of the child process. If the child process fails to spawn due to errors, then the value is undefined
. When the child process exits, then the value is undefined
after the exit
event is emitted.
child.stdout
// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.stdout.on('data', (data) => {
console.log(`Received chunk ${data}`)
})
child.stderr
A NodeJS.ReadableStream | null
that represents the child process’s stderr. If the child was spawned with options.stdio[2] set to anything other than ‘pipe’, then this will be null
. When the child process exits, then the value is null
after the exit
event is emitted.
Event: ‘spawn’
Emitted once the child process has spawned successfully.
Event: ‘exit’
Returns:
- number - Contains the exit code for the process obtained from waitpid on posix, or GetExitCodeProcess on windows.
Emitted after the child process ends.
Event: ‘message’
Emitted when the child process sends a message using .