After looking at the basics of Node for the last two days, I finally got down to the nitty – and – nitty – stuff. Let’s take a look at global objects in Node.

A global object

Just like Windows in the browser, Node places variable methods that are globally accessible in global. We can type global in node’s REPL to see what’s in it.

In global we can find several global objects: console, Process, global, and so on. Of course, there are several global functions in global: timer correlation, Buffer, require. Then there are the global variables: __filename, __dirname. Module, exports, etc., which we can consider pseudo-global variables.

In browsers, we define variables directly to the window, but in Node we define variables directly to the module, which is not available in global. If you want to try to get global. A = 1; This will get the defined variable from the global variable.

Second, the console

We should use a lot of this, after all, debugging console is still very reliable things. Console.log () and console.info() should be the most frequently used. Time (label) and console.timeEnd(label). The labels are a pair. The same label will start a timer and stop a timer and time it. This also feels like a more practical console.

Third, the process

The process object is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require().

Take a look at the documentation for the description of Process, which is a global object that provides some information about the current process and how to control it. And this is a global object, so you don’t need require when you use it. So we can see that this object is used to view the information and control function of the current process. Let’s take a look at some of the variables and methods that are commonly used, rather than the events of process.

First, let’s take a look at the first process.arch, which literally describes the processor architecture and can be tested in the REPL.

Mine is the X64 processor architecture

The second, process.argv, is an array containing the command line arguments to start the Node.js process. Let’s write our own code to have a look:


The first line is the command to start the file, which is where Node is located, the second line is the path to the file that is currently being executed, and then the parameters that follow. The first two entries of process.argv contain the node address and the address of the execution file, followed by the various parameters. Argv0 is a read-only copy of the value of the argv[0] argument passed to node.js at startup. The process.argv documentation also states that the first parameter is process.execPath, so we can clearly see what this parameter means. Without further notice, the next one to look at is process.execArgv, which returns the specific command-line options behind the Node command.

You can see that this variable stores the specific command line options that follow the Node command.

Third, process.env, which stores our user environment information, such as PATH, HOME, etc., are in this.

Process.cwd () returns the directory in which the Node.js process is currently working.


Fifth, Process. nextTick, this one is going to compare with setImmediate on Timer, but I won’t go into that because it feels like it.


Today probably summed up so much, or good digestion, there is wrong, hope to put forward, mutual encouragement progress, thank you.

PS: No, I think I want to amway a wave of TMUx, Linux used up a thief cool…..