Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js Process
Node.js provides the facility to get process information such as process id, architecture, platform, version, release, uptime, upu usage etc. It can also be used to kill process, set uid, set groups, unmask etc.
The process is a global object, an instance of EventEmitter, can be accessed from anywhere.
Node.js Process Properties
A list of commonly used Node.js process properties are given below.
Property | Description |
---|---|
arch | returns process architecture: 'arm', 'ia32', or 'x64' |
args | returns commands line arguments as an array |
env | returns user environment |
pid | returns process id of the process |
platform | returns platform of the process: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32' |
release | returns the metadata for the current node release |
version | returns the node version |
versions | returns the node version and its dependencies |
Node.js Process Properties Example
Let's see the simple process example to print architecture, pid, platform and version of the process.File: process_example1.js
- console.log(`Process Architecture: ${process.arch}`);
- console.log(`Process PID: ${process.pid}`);
- console.log(`Process Platform: ${process.platform}`);
- console.log(`Process Version: ${process.version}`);
Open Node.js command prompt and run the following code:
- node process_example1.js
Let's see another process example to print command line arguments. Here node is considered as the first argument, filename is considered as the second argument and actual command line arguments are considered as third, fourth, fifth and so on.
File: process_example2.js
- process.argv.forEach((value, index, array) => {
- console.log(`${index}: ${value}`);
- });
Open Node.js command prompt and run the following code:
- node process_example2.js
Node.js Process Functions
A list of commonly used Node.js process functions are given below.
Function | Description |
---|---|
cwd() | returns path of current working directory |
hrtime() | returns the current high-resolution real time in a [seconds, nanoseconds] array |
memoryUsage() | returns an object having information of memory usage. |
process.kill(pid[, signal]) | is used to kill the given pid. |
uptime() | returns the Node.js process uptime in seconds. |
Node.js Process Functions Example
Let's see the process example to print current working directory and uptime of the process.
File: process_example3.js
- console.log(`Current directory: ${process.cwd()}`);
- console.log(`Uptime: ${process.uptime()}`);
Open Node.js command prompt and run the following code:
- node process_example3.js