Familiarize yourself with Node.js: What happens when creating a new module in Node.js?

Tram Ho

Hello friends!
On the occasion of Tet holiday, Spring comes, wishing everyone lots of luck, happiness, talent and fortune!
Come to the topic of this article yet!

1. What happens when creating a new module in Node.js:

First, I want to pose an interesting problem when creating a new .js module in Node.js. So the question is: What happens when we create a new module?
First, I will create the app.js module with a few simple lines of code.

Of course, when running in the terminal, everyone will immediately know the other impossible result is Hello Node.js. But what happened before we got the result.
Add the following code to the first line of the app.js module (must be the first line).

Running the above terminal, the syntax error will be sure. But is there something we need to care about here? Yes, we can see the code line var whatHappenning =; wrapped in a function that looks familiar, doesn’t it ?.
Yeah, yeast. Let’s analyze again. With the above error, we can detect that the code in the module will be wrapped in a function with arguments. Now, I will rewrite the code as follows (of course, remove the syntax error line code):

If you, have studied through javascript, surely, you will realize this is the function that will be executed right after initialization. This means that the lines of code they write in a module are not executed directly but before executing it will be wrapped in the function before being executed. Now I go back to the previous post a bit. Obviously, the previous articles mentioned require, exports, module, __filename, __dirname , we use these but do not know where it comes from. Now I will explain more here.
Node.js is also based on the javascript engine v8 like on Chrome browser (Chrome browser) or other browsers that use javascripts engine v8. However, on the Node.js environment, there is no DOM object as in the browser, but Node.js provides us with a global object . That is why we cannot call console.log (window); in Node.js, we can instead call console.log (global); . The global object in Node.js also has the same methods as the window object on the browser, for example, console.log, setTimeout, setInterval, clearTimeout, clearInterval, etc. Do the arguments in the function wrap our code before executing it belong to the global object or how is their scope? In fact, these arguments have no global scope but only the scope in the module we call. And these arguments are passed into each module so we can call them when needed.
The explanation in this section will help us know, why they can use the require, exports, module, __filename, __dirname methods in each module we create.

2. Introduction and use of some modules / libraries in Node.js:

Module Path: The Path Module provides tools to interact with the file (file) and directory path (path) of the file.
Using:

The terminal result lists the methods that can be used in the path module . If you want to get information about the path of the file use path.parse (__ filename) ; to get dirname information using path.dirname (__ filename) ; to get basename information using path.basename (__ filename) ; …

OS Module: The OS module provides methods and properties that work with components related to the operating system, such as memory, CPU information, network, operating platform, etc.

3. Conclusion

A closer look at how a Node.js module works is a great help in the process of building an application with Node.js. In this article, I also introduce more about some modules / libraries in Node.js and how to use them. You can find out more in the docs of Node.js
Posts inevitable mistakes, hope to receive your comments to improve the article. Thank you.

Reference source:
+ Node.js: https://nodejs.org/en/docs/

Share the news now

Source : Viblo