Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is process in Node.js

Tags:

node.js

I was learning Node.js and came across something called Node.js process. After some research done I found this statement: process is an object referencing to the actual computer process running a Node program and allows for access to command-line arguments and much more. here https://www.codecademy.com/articles/what-is-node. So, the question is "Is it true that process is something within which our Node.js is run and process itself represents processor of a computer?"

like image 236
SAM Avatar asked Jan 24 '26 01:01

SAM


2 Answers

Each program running on a computer represents a process. It's a top level task that an operating system such as Windows or Linux uses to encapsulate a running program. Among other things, a process contains:

  1. Code that is running
  2. Memory that is allocated to it by the OS
  3. Files or sockets that it has open
  4. One or more threads running within the process

When a process exits (or crashes), the operating system automatically cleans up resources owned by that process (closes files/sockets, returns memory back to the OS, shuts-down threads, etc...).

The operating system shares the CPU cores on the computer among all the different processes and threads in those processes running on the computer. In this way, even if lots of programs are all trying to use the CPU at once, they all get some of the CPU time and all appear to be making forward progress. In reality, one gets to run for a little bit of time, then the next, then the next and so on, but those time slices can be so small that they all appear to be running together.

The term process is an operating system term and not a node.js term. The process module in node.js is a central place where the designers of node.js put a bunch of methods that relate to the overall process such as process.exit() which exits the application and thus stops the process or process.env which gives you access to the environment variables for your program or process.argv which gives you access to the command line arguments your process was started with and so on... These are all things that apply to your overall program running.

like image 71
jfriend00 Avatar answered Jan 25 '26 20:01

jfriend00


First, you need to understand node is a program and it is a host environment for running javascript. You can then run node from the command line like this to execute the program:

$ node hello.js
Hello world

If you run node without giving it a file, it provides you with a prompt at which you can type JavaScript code and immediately see the result.

$ node
> 1 + 1
2
> [-1, -2, -3].map(Math.abs)
[1, 2, 3]
> process.exit(0)
$

The process binding is available globally in Node. It provides various ways to inspect and manipulate the current program. The exit method ends the process and can be given an exit status code, which tells the program that started node (in this case, the command line shell) whether the program completed successfully (code zero) or encountered an error (any other code).

process.env allows you to access the environment of node and process.argv allows you to access the arguments passed to the node command. For example, if showargv.js contains the statement console.log(process.argv), you could run it like this:

$ node showargv.js one --and two
["node", "/tmp/showargv.js", "one", "--and", "two"]

The term "process", indeed, is not only a node term but also an "operating system" term, as mentioned by @jfriend00. Each program has its processes. It is important to remember that the process in node allows users to obtain node related info and have some functions to control the node's behaviour.

The process module in node.js is a central place where the designers of node.js put a bunch of methods that relate to the overall process. @jfriend00

like image 44
よまる Avatar answered Jan 25 '26 20:01

よまる



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!