1
00:00:00,180 --> 00:00:04,500
In this video, we are going to learn Node.js modules.

2
00:00:04,680 --> 00:00:06,480
So what is a Node.js module?

3
00:00:06,570 --> 00:00:11,610
We can consider Node.js modules to be the same as JavaScript libraries.

4
00:00:11,970 --> 00:00:16,590
It is a set of functions that we want to include our applications.

5
00:00:16,920 --> 00:00:20,280
So let's make this logger file as a Node.js module.

6
00:00:20,400 --> 00:00:25,770
As you remember that in the last video we have created the local JS and local function, but now we

7
00:00:25,770 --> 00:00:29,550
can create these logger operation as a Node.js module.

8
00:00:29,670 --> 00:00:35,880
In order to do that, we are going to change these method with the method which they should be the exports.

9
00:00:37,430 --> 00:00:37,800
You would.

10
00:00:37,880 --> 00:00:40,100
Yes, exports cost.

11
00:00:40,100 --> 00:00:44,570
And I would like to change the name of the method which is the right logo.

12
00:00:45,620 --> 00:00:51,770
And now we don't need to invoke this function inside of the file.

13
00:00:53,630 --> 00:00:56,350
It should be export, not exports.

14
00:00:57,700 --> 00:01:04,360
So as you can see that this is the method body that we can use the export keyword and it is basically

15
00:01:04,360 --> 00:01:06,710
exporting these functions to the other files.

16
00:01:06,730 --> 00:01:09,820
That means this becomes a Node.js modules.

17
00:01:10,000 --> 00:01:16,240
So now how we can use these Node.js module into other files in order to do that, please create an index

18
00:01:16,240 --> 00:01:18,580
case and into the index case.

19
00:01:18,670 --> 00:01:22,840
I'm going to write a function, for example, main function.

20
00:01:30,500 --> 00:01:37,940
And in the main function, we can basically make our log operation, which is the main function.

21
00:01:39,040 --> 00:01:39,730
Starts.

22
00:01:40,060 --> 00:01:42,640
And after that, we can.

23
00:01:43,530 --> 00:01:44,760
Bite the right look.

24
00:01:45,180 --> 00:01:50,630
So when I write the right look, operation method, as you can see that it is going to add inputs to

25
00:01:50,630 --> 00:01:53,550
a statement from the logger file.

26
00:01:53,550 --> 00:02:03,150
So if you click this IntelliSense keyword, you can see that it is basically import logger class and

27
00:02:03,150 --> 00:02:07,140
logger JavaScript file and import write log method.

28
00:02:07,320 --> 00:02:13,230
So by this way, we can use this method into our main function from the index JS file.

29
00:02:13,350 --> 00:02:17,400
So in order to do that, I'm going to pass two parameters.

30
00:02:17,400 --> 00:02:19,320
I would like to paste two parameters.

31
00:02:19,440 --> 00:02:26,220
So instead of we are getting the arguments, we can change these local function to get the name and

32
00:02:26,220 --> 00:02:27,750
surname as a parameter.

33
00:02:27,750 --> 00:02:30,660
So let's make a little bit change.

34
00:02:30,660 --> 00:02:34,590
So name and surname is created as a parameter for this function.

35
00:02:34,590 --> 00:02:44,490
So in order to use this function, I'm going to set a these name and surname from the parameters.

36
00:02:44,490 --> 00:02:46,560
Now we can remove this line of code.

37
00:02:47,500 --> 00:02:48,120
Okay.

38
00:02:48,130 --> 00:02:52,100
So as you can see that this very basic Node.js model.

39
00:02:52,120 --> 00:02:57,210
It is getting the name and surname as a parameter and it will make the log message.

40
00:02:57,220 --> 00:03:05,740
But we expose this right lock method into the indexes and to do so in order to use this function.

41
00:03:05,830 --> 00:03:08,680
As you can see that we should provide the name and surname.

42
00:03:08,680 --> 00:03:13,150
So I'm going to give my name and surname basing this function.

43
00:03:16,120 --> 00:03:22,300
And after that, we will call these mean method to invoke all these operations.

44
00:03:22,810 --> 00:03:23,470
Okay.

45
00:03:23,650 --> 00:03:27,250
Now I'm going to run the node command node.

46
00:03:28,240 --> 00:03:31,540
Next JS file and see what's happened.

47
00:03:32,290 --> 00:03:39,670
So when we run this not comment, we get exception, which is the cannot use import statement outside

48
00:03:39,670 --> 00:03:40,360
a module.

49
00:03:40,450 --> 00:03:42,950
So what is exception is throwing?

50
00:03:42,970 --> 00:03:49,270
So if you scroll up you can see the warning to load an is module.

51
00:03:49,300 --> 00:03:52,450
We should set the type module in package JSON file.

52
00:03:52,540 --> 00:03:58,930
So this is related with the module explanation in the Node.js function that I'm going to explain in

53
00:03:58,930 --> 00:03:59,980
the upcoming videos.

54
00:03:59,980 --> 00:04:03,100
But I can say that Node.js has two types of modules.

55
00:04:03,100 --> 00:04:04,690
One is the common JS.

56
00:04:04,720 --> 00:04:07,600
Another is the script name version.

57
00:04:07,600 --> 00:04:09,940
So we will use a script modules.

58
00:04:09,940 --> 00:04:15,160
So to activate it, we should set the type module in the package JSON file.

59
00:04:15,220 --> 00:04:19,810
So let me add the package JSON file for now and I will explain in the outcome could use.

60
00:04:19,900 --> 00:04:25,360
But you can add the package JSON file after that.

61
00:04:27,240 --> 00:04:34,050
We can set the type and you can see that there is two types of Node.js modules, common JS and module.

62
00:04:34,080 --> 00:04:38,840
We are going to use module in order to activate the latest script codes.

63
00:04:38,850 --> 00:04:47,820
So let's save this file and this time in the local file can be exported method with using the export

64
00:04:48,270 --> 00:04:56,280
keyword and after that index file can be used this local operation with importing the logger file.

65
00:04:56,530 --> 00:05:00,390
Okay, now after that, let's run this command again.

66
00:05:00,390 --> 00:05:02,580
Note index is hit enter.

67
00:05:03,750 --> 00:05:06,870
So this time again we got another exception.

68
00:05:06,870 --> 00:05:09,510
So this is also important exception.

69
00:05:09,690 --> 00:05:14,550
So as you can see the message did you mean to import logs?

70
00:05:14,550 --> 00:05:21,540
So sometimes in some cases it is explicitly not that the full folder name.

71
00:05:21,550 --> 00:05:28,620
So that means when we are importing this logger operation, we should provide the logger dot js file.

72
00:05:28,890 --> 00:05:30,090
So this is important.

73
00:05:30,090 --> 00:05:36,180
Please save this file import statement because when we are adding these import statement with using

74
00:05:36,180 --> 00:05:42,300
the values to do code IntelliSense, it doesn't include this JavaScript file and we should add the JavaScript

75
00:05:42,300 --> 00:05:43,320
file explicitly.

76
00:05:43,830 --> 00:05:48,420
So after that, after make this change and save these indexes file.

77
00:05:49,510 --> 00:05:52,570
If you run the note indexes comma hit enter.

78
00:05:52,600 --> 00:05:56,230
We can get the success response in our case.

79
00:05:56,230 --> 00:06:02,650
So as you can see that the first log is come from the main function starts and the second log is comes

80
00:06:02,650 --> 00:06:06,710
from the our right log operation, which is exposing from logs.

81
00:06:06,730 --> 00:06:12,740
We can right click go to definition and see the log operation, which is your name and surname is Mammoth

82
00:06:12,760 --> 00:06:13,170
Square.

83
00:06:14,290 --> 00:06:14,710
Okay.

84
00:06:15,070 --> 00:06:21,310
As you can see that we understand how Node.js model works and how we can use modules in our Node.js

85
00:06:21,310 --> 00:06:22,120
application.

86
00:06:22,390 --> 00:06:27,880
If we examine the Node.js lambda function, for example, I have put the calculator lambda function

87
00:06:27,880 --> 00:06:28,840
code in here.

88
00:06:28,930 --> 00:06:34,920
You can see that we have all of the exposed 100 method in a common module version.

89
00:06:34,930 --> 00:06:40,130
You can see that this time it is using exports and it is exporting 100 method.

90
00:06:40,150 --> 00:06:45,850
So if you look at the lambda function, this is the runtime Node.js runtime is running the handle method

91
00:06:45,850 --> 00:06:50,430
when invoking any location comes to lambda function.

92
00:06:50,440 --> 00:06:56,740
So we are exposing these 100 method with using these exports 100 method body.

93
00:06:56,920 --> 00:07:04,180
But we can change these export 100 which we must equip Node.js module with adding package JSON we will

94
00:07:04,180 --> 00:07:05,590
do in the upcoming videos.

95
00:07:05,590 --> 00:07:12,940
But if we examine the our calculator JavaScript file of the lambda function, it is basically exports

96
00:07:12,940 --> 00:07:16,600
the one handed method after that one event in the lambda function.

97
00:07:16,600 --> 00:07:20,470
This will trigger to handle method with incoming event JSON object.

98
00:07:20,590 --> 00:07:25,000
So we have learned the basics of lambda node.js functions and the modules.

99
00:07:25,030 --> 00:07:31,530
As you can see that we have create a Node.js module and use it this module into our index file.
