Suggest an editImprove this articleRefine the answer for “What is process?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`process`** is a global Node.js object, available in any module without an import, giving access to the PID, the Node.js version, the platform, environment variables (`process.env`), launch arguments (`process.argv`), the stdin/stdout/stderr streams and process-exit events. **Key point:** `process.exit(0)` means a successful exit; any other code signals an error.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `process` is > `process` is an object provided by Node.js that describes the **currently running** JavaScript process in the Node.js environment. It's available **anywhere in the program**, with no need to import it: ```javascript console.log(process); ``` An analog in other languages: - in Python, `sys` + `os`; - in C, `getpid()`, `argv`, `exit()`, and so on. ## 2. Main capabilities of `process` | Category | What it does | |---|---| | Process information | PID, Node.js version, architecture, platform | | Environment variables | `process.env` | | Launch arguments | `process.argv` | | Standard streams | `process.stdin`, `process.stdout`, `process.stderr` | | Ending the process | `process.exit()` | | Lifecycle events | `beforeExit`, `exit`, `uncaughtException`, `SIGINT` | | Resource information | Memory, CPU, uptime, the current directory | | Node.js flags and options | `process.execPath`, `process.execArgv` | ## 3. Main properties and methods of `process` ### `process.pid` → The ID of the current process (Process ID) ```javascript console.log(process.pid); // for example, 9342 ``` ### `process.version` → The Node.js version ```javascript console.log(process.version); // v22.8.0 ``` ### `process.platform` → Which platform Node.js is running on ```javascript console.log(process.platform); // 'win32' | 'linux' | 'darwin' ``` ### `process.arch` → The processor architecture ```javascript console.log(process.arch); // 'x64', 'arm64', 'ia32', etc. ``` ### `process.cwd()` → The current working directory ```javascript console.log(process.cwd()); // /Users/tim/projects/app ``` ### `process.env` → An object holding all **environment variables** ```javascript console.log(process.env.NODE_ENV); // 'production' console.log(process.env.PATH); // the system PATH ``` Used for configuration: ```javascript if (process.env.NODE_ENV === 'production') { console.log('Running in production'); } ``` ### `process.argv` → The command-line arguments Node.js was started with ```javascript node app.js dev 8080 ``` ```javascript console.log(process.argv); // [ '/usr/local/bin/node', '/path/to/app.js', 'dev', '8080' ] ``` Often used for CLI tools. ### `process.memoryUsage()` → Shows how much memory the app is using ```javascript console.log(process.memoryUsage()); /* { rss: 24576000, heapTotal: 5464064, heapUsed: 3661728, external: 8704 } */ ``` ### `process.uptime()` → Time (in seconds) since the process started ```javascript console.log(process.uptime()); // 5.312 ``` ### `process.exit([code])` → Ends the process ```javascript process.exit(0); // a normal exit process.exit(1); // an exit with an error ``` Code `0` means success, any other code means an error (for example, used in CI/CD or shell scripts). ### `process.on(event, handler)` → Subscribing to process events Examples: #### When the program ends: ```javascript process.on('exit', (code) => { console.log('Exiting with code:', code); }); ``` #### An uncaught error: ```javascript process.on('uncaughtException', (err) => { console.error('Uncaught error:', err); }); ``` #### When Ctrl+C is pressed: ```javascript process.on('SIGINT', () => { console.log('Process interrupted by user'); process.exit(); }); ``` ### `process.stdin`, `process.stdout`, `process.stderr` → Input/output streams ```javascript process.stdout.write('Enter your name: '); process.stdin.on('data', (data) => { console.log(`Hello, ${data.toString().trim()}!`); }); ``` This is a basic way to work with the console with no external libraries. ## 4. An example in a real Node.js application ```javascript console.log(`Started with PID: ${process.pid}`); console.log(`OS: ${process.platform}`); console.log(`Node.js: ${process.version}`); console.log(`Memory: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`); process.on('exit', (code) => { console.log(`Process exited with code ${code}`); }); setTimeout(() => { console.log('Exiting in 2 seconds...'); process.exit(0); }, 2000); ``` ## 5. Why `process` matters - It lets you interact with the **operating system**; - it manages the **environment and configuration** (through `env`); - it gives access to **CLI parameters**; - it manages **input/output streams**; - it lets you **catch errors** and exit cleanly. ## 6. Common use cases | Scenario | Used for | |---|---| | Environment configuration | `process.env.NODE_ENV` | | CLI applications | `process.argv` | | Debugging and logging | `process.memoryUsage()`, `process.uptime()` | | Ending the process | `process.exit()` | | Error handling | `process.on('uncaughtException', handler)` | | Graceful shutdown | `process.on('SIGINT', cleanup)` | ## 7. Quick summary | Property / Method | Purpose | |---|---| | `process.pid` | The process ID | | `process.platform` | The OS platform | | `process.env` | Environment variables | | `process.argv` | Launch arguments | | `process.cwd()` | The current directory | | `process.memoryUsage()` | Memory usage | | `process.uptime()` | How long the process has run | | `process.exit([code])` | Ending the process | | `process.on(event, cb)` | Subscribing to process events | | `process.stdin / stdout / stderr` | Input/output streams | ## In one sentence: > `process` is Node.js's global object that gives access to information about the running process, environment variables, command-line arguments, input/output streams, and control over how the program exits.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.