How module works in nodejs?

Let’s create a test.js file and type console.log(arguments), what will return?

node

  1. undefined
  2. something else, but what? 🙂

the answer is

node

it is a array of 5 items?

So what are they?

To answer this question, we need to understand how module works in nodejs. Nodejs wraps the module into the function before compiling the modules and that function looks like below. As you can see, the function has 5 parameters, that explains what we saw in the previous picture.

node2

Hard to believe? Let put a break point at console.log(arguments) and debug the code, then you can see the 5 parameters are in the local scope of the current module. Please note, the first line and third line are comments.

node3.PNG

node4