Node.js v22.19.0 documentation
- Node.js v22.19.0
-
Table of contents
- Process
- Process events
process.abort()
process.allowedNodeEnvironmentFlags
process.arch
process.argv
process.argv0
process.availableMemory()
process.channel
process.chdir(directory)
process.config
process.connected
process.constrainedMemory()
process.cpuUsage([previousValue])
process.cwd()
process.debugPort
process.disconnect()
process.dlopen(module, filename[, flags])
process.emitWarning(warning[, options])
process.emitWarning(warning[, type[, code]][, ctor])
process.env
process.execArgv
process.execPath
process.execve(file[, args[, env]])
process.exit([code])
process.exitCode
process.features.cached_builtins
process.features.debug
process.features.inspector
process.features.ipv6
process.features.require_module
process.features.tls
process.features.tls_alpn
process.features.tls_ocsp
process.features.tls_sni
process.features.typescript
process.features.uv
process.finalization.register(ref, callback)
process.finalization.registerBeforeExit(ref, callback)
process.finalization.unregister(ref)
process.getActiveResourcesInfo()
process.getBuiltinModule(id)
process.getegid()
process.geteuid()
process.getgid()
process.getgroups()
process.getuid()
process.hasUncaughtExceptionCaptureCallback()
process.hrtime([time])
process.hrtime.bigint()
process.initgroups(user, extraGroup)
process.kill(pid[, signal])
process.loadEnvFile(path)
process.mainModule
process.memoryUsage()
process.memoryUsage.rss()
process.nextTick(callback[, ...args])
process.noDeprecation
process.permission
process.pid
process.platform
process.ppid
process.ref(maybeRefable)
process.release
process.report
process.report.compact
process.report.directory
process.report.filename
process.report.getReport([err])
process.report.reportOnFatalError
process.report.reportOnSignal
process.report.reportOnUncaughtException
process.report.excludeEnv
process.report.signal
process.report.writeReport([filename][, err])
process.resourceUsage()
process.send(message[, sendHandle[, options]][, callback])
process.setegid(id)
process.seteuid(id)
process.setgid(id)
process.setgroups(groups)
process.setuid(id)
process.setSourceMapsEnabled(val)
process.setUncaughtExceptionCaptureCallback(fn)
process.sourceMapsEnabled
process.stderr
process.stdin
process.stdout
process.throwDeprecation
process.threadCpuUsage([previousValue])
process.title
process.traceDeprecation
process.umask()
process.umask(mask)
process.unref(maybeRefable)
process.uptime()
process.version
process.versions
- Exit codes
- Process
-
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
Process#
Source Code: lib/process.js
The process
object provides information about, and control over, the current
Node.js process.
import process from 'node:process';
const process = require('node:process');
Process events#
The process
object is an instance of EventEmitter
.
Event: 'beforeExit'
#
The 'beforeExit'
event is emitted when Node.js empties its event loop and has
no additional work to schedule. Normally, the Node.js process will exit when
there is no work scheduled, but a listener registered on the 'beforeExit'
event can make asynchronous calls, and thereby cause the Node.js process to
continue.
The listener callback function is invoked with the value of
process.exitCode
passed as the only argument.
The 'beforeExit'
event is not emitted for conditions causing explicit
termination, such as calling process.exit()
or uncaught exceptions.
The 'beforeExit'
should not be used as an alternative to the 'exit'
event
unless the intention is to schedule additional work.
import process from 'node:process';
process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code: ', code);
});
process.on('exit', (code) => {
console.log('Process exit event with code: ', code);
});
console.log('This message is displayed first.');
// Prints:
// This message is displayed first.
// Process beforeExit event with code: 0
// Process exit event with code: 0
const process = require('node:process');
process.on('beforeExit', (code) => {
console.log('Process beforeExit event with code: ', code);
});
process.on('exit', (code) => {
console.log('Process exit event with code: ', code);
});
console.log('This message is displayed first.');
// Prints:
// This message is displayed first.
// Process beforeExit event with code: 0
// Process exit event with code: 0
Event: 'disconnect'
#
If the Node.js process is spawned with an IPC channel (see the Child Process
and Cluster documentation), the 'disconnect'
event will be emitted when
the IPC channel is closed.
Event: 'exit'
#
code
<integer>
The 'exit'
event is emitted when the Node.js process is about to exit as a
result of either:
- The
process.exit()
method being called explicitly; - The Node.js event loop no longer having any additional work to perform.
There is no way to prevent the exiting of the event loop at this point, and once
all 'exit'
listeners have finished running the Node.js process will terminate.
The listener callback function is invoked with the exit code specified either
by the process.exitCode
property, or the exitCode
argument passed to the
process.exit()
method.
import process from 'node:process';
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
const process = require('node:process');
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
Listener functions must only perform synchronous operations. The Node.js
process will exit immediately after calling the 'exit'
event listeners
causing any additional work still queued in the event loop to be abandoned.
In the following example, for instance, the timeout will never occur:
import process from 'node:process';
process.on('exit', (code) => {
setTimeout(() => {
console.log('This will not run');
}, 0);
});
const process = require('node:process');
process.on('exit', (code) => {
setTimeout(() => {
console.log('This will not run');
}, 0);
});
Event: 'message'
#
message
<Object> | <boolean> | <number> | <string> | <null> a parsed JSON object or a serializable primitive value.sendHandle
<net.Server> | <net.Socket> anet.Server
ornet.Socket
object, or undefined.
If the Node.js process is spawned with an IPC channel (see the Child Process
and Cluster documentation), the 'message'
event is emitted whenever a
message sent by a parent process using childprocess.send()
is received by
the child process.
The message goes through serialization and parsing. The resulting message might not be the same as what is originally sent.
If the serialization
option was set to advanced
used when spawning the
process, the message
argument can contain data that JSON is not able
to represent.
See Advanced serialization for child_process
for more details.