Specific use of node.js global variables

Specific use of node.js global variables

Global Object

All modules can be called

  1. global: represents the global environment of Node, similar to the window object in the browser.
  2. process: points to Node's built-in process module, allowing developers to interact with the current process.
  3. For example, if you directly enter node in DOS or terminal window, you will enter the NODE command line mode (REPL environment). To exit, enter process.exit();
  4. console: points to the built-in console module of Node, which provides standard input and standard output functions in the command line environment.

Global Functions

Timer functions: There are 4 timer functions: setTimeout(), clearTimeout(), setInterval(), clearInterval().

require: used to load modules.

It was often seen in King Qi's house and heard several times in Cui Jiu's hall.

Global variables

  • _filename: points to the name of the script currently running.
  • _dirname: points to the directory where the currently running script is located.

Quasi-global variables

The local variables inside the module point to different objects depending on the module, but they are applicable to all modules and can be regarded as pseudo-global variables, mainly module, module.exports, exports, etc.

The module variable refers to the current module. The module.exports variable represents the interface exported by the current module. When other files load the module, they actually read the module.exports variable.

  • module.id The module identifier, usually the module's file name.
  • module.filename The filename of the module.
  • module.loaded returns a boolean value indicating whether the module has finished loading.
  • module.parent returns the module that uses this module.
  • module.children returns an array of other modules that this module uses.

Global sample code

insert image description here

// Include the full path of the file name console.log(__filename);
// The path to the file (excluding the file name)
console.log(__dirname);

// Timing function, usage is similar to the timing function in the browser var timer = setTimeout(function(){
    console.log(123);
},1000);

setTimeout(function(){
    clearTimeout(timer);
},2000);

// There is no window object in Node.js, but there is a similar object global, which can be omitted when accessing global members
global.console.log(123456);

// argv is an array. By default, the first two items are: the path of the Node.js environment; the full path of the currently executed js file // Starting from the third parameter, it represents the command line parameters console.log(process.argv);
// Print the current system architecture (64-bit or 32-bit)
console.log(process.arch);

This is the end of this article about the specific use of node.js global variables. For more relevant node.js global variables content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Usage of Node.js http module
  • Nodejs Exploration: In-depth understanding of the principle of single-threaded high concurrency
  • Understanding what Node.js is is so easy
  • AsyncHooks asynchronous life cycle in Node8
  • Nodejs error handling process record
  • The whole process of node.js using express to automatically build the project
  • How to use shell scripts in node
  • The core process of nodejs processing tcp connection
  • Detailed explanation of Nodejs array queue and forEach application
  • Comparing Node.js and Deno

<<:  How to quickly delete all tables in MySQL without deleting the database

>>:  Detailed explanation of using Docker to build a development environment for Laravel and Vue projects

Recommend

How to remove the dividing line of a web page table

<br />How to remove the dividing lines of a ...

Vue+Vant implements the top search bar

This article example shares the specific code of ...

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

Implementation code of html floating prompt box function

General form prompts always occupy the form space...

Analysis of Hyper-V installation CentOS 8 problem

CentOS 8 has been released for a long time. As so...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

Detailed explanation of Vue monitoring attribute graphic example

Table of contents What is the listener property? ...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...

Concat() of combined fields in MySQL

Table of contents 1. Introduction 2. Main text 2....

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

Web Design: The Accurate Location and Use of Massive Materials

Three times of memorization allows you to remembe...

HTML tutorial, easy to learn HTML language (2)

*******************Introduction to HTML language (...