improve process.md

This commit is contained in:
thomasabishop 2023-05-03 07:08:13 +01:00
parent d5a2fa95fb
commit af3e4f2861
2 changed files with 9 additions and 7 deletions

View file

@ -8,17 +8,17 @@ tags:
# The `process` module in Node.js
`process` is a core module and a global object accessible from anywhere in a Node.js application. It contains functionality that allows us to interact with information about the current process instance.
`process` is a core module and a global object accessible from anywhere in a Node.js application. It contains functionality that allows us to interact with information about the current process instance of Node.
For example, we can use it to get environment information, read environment variables, communicate with the terminal and exit the current process.
For example, we can use it to get environment information, read environment variables, parse arguments that are passed to the run command, communicate with the terminal and exit the current process.
## Managing runtime environments
See [Managing Environments](/Programming_Languages/Node/Architecture/Managing_environments.md).
See [Managing Environments](/Programming_Languages/NodeJS/Architecture/Managing_environments.md).
## Accessing arguments: `process.argv`
We can use `process.argv` to return an array containing the command-line arguments passed when a Node process was launched. This could be a whole-app entrypoint (i.e. `index.js`) or a single file we are running.
We can use `process.argv` to return an array containing the command-line arguments passed when a Node process is launched. This could be a whole-app entrypoint (i.e. `index.js`) or a single file we are running.
For instance if we run the following file:
@ -38,7 +38,9 @@ We get:
]
```
The first value is a reference to the Node runtime binary. The second is the file that was passed to node.
The first item is the last executed process. The second is the array of arguments.
In the array, the first value is a reference to the Node runtime binary. The second is the file that was passed to Node.
If we passed in a parameter we would get that too:
@ -56,7 +58,7 @@ Gives us:
]
```
When writing command line Node applications we could easily write functions that parse standard input. For example:
When writing Node CLI applications we can write functions that parse standard input. For example:
```js
function parseStandardInput(flag) {
@ -87,7 +89,7 @@ node alpha.js
Hello from file
```
Typically we want to write and read data that the user provides. To do this we need to wait on that event. So we use Node's asynchonous [event listeners](/Programming_Languages/Node/Modules/Core/Node_JS_events_module.md). We wait on the `data` event:
Typically we want to write and read data that the user provides. To do this we need to wait on that event. So we use Node's asynchonous [event listeners](/Programming_Languages/NodeJS/Modules/Core/events.md). We wait on the `data` event:
```js
process.stdin.on("data", (data) => {