1
00:00:00,000 --> 00:00:04,740
[MUSIC]

2
00:00:04,740 --> 00:00:09,065
In this lesson, we will learn about
how we can write simple notifications.

3
00:00:09,065 --> 00:00:11,454
We'll also learn about Node modules and

4
00:00:11,454 --> 00:00:16,885
how Node modules enable us to organize
our applications into multiple files.

5
00:00:16,885 --> 00:00:20,550
We'll also learn about the different
kinds of Node modules and

6
00:00:20,550 --> 00:00:26,450
how we can import Node modules into
our files of our Node application and

7
00:00:26,450 --> 00:00:29,260
make use of them within our application.

8
00:00:29,260 --> 00:00:34,640
First, let's learn about Node modules in
a little more detail in this lecture.

9
00:00:36,720 --> 00:00:39,320
JavaScript, when it is first designed

10
00:00:39,320 --> 00:00:43,900
was meant to be a scripting language
to be used within the browser.

11
00:00:43,900 --> 00:00:49,510
So the small realm within which it was
supposed to be used is the browser.

12
00:00:49,510 --> 00:00:54,695
Now, as you understand from this
specialization, JavaScript has gone

13
00:00:54,695 --> 00:01:00,822
way beyond its original intention, and now
is being used for writing applications.

14
00:01:00,822 --> 00:01:04,688
Both to be run using frameworks
within the browser, and

15
00:01:04,688 --> 00:01:09,880
also to run applications on the server
side, as we will see in this course.

16
00:01:11,020 --> 00:01:16,100
JavaScript originally was never
designed with any common libraries.

17
00:01:16,100 --> 00:01:20,907
If you look at standard programming
languages like C, C++, Java, and so on,

18
00:01:20,907 --> 00:01:24,374
they all have standard libraries
that enable you to access

19
00:01:24,374 --> 00:01:26,730
the underlying hardware.

20
00:01:26,730 --> 00:01:32,170
And also provide a structured way of
organization the application into

21
00:01:32,170 --> 00:01:36,880
multiple files and then combining them
together when you create an application.

22
00:01:36,880 --> 00:01:41,580
JavaScript never had any of
this support when it began.

23
00:01:41,580 --> 00:01:45,850
Because as we understand
JavaScript was not designed for

24
00:01:45,850 --> 00:01:48,330
the purpose for
which it is being used today.

25
00:01:49,420 --> 00:01:50,500
But of course,

26
00:01:50,500 --> 00:01:55,710
people understood the difficulties
when you need to expand JavaScript

27
00:01:55,710 --> 00:02:01,340
beyond a single file which is used as
a scripting language for the browser.

28
00:02:01,340 --> 00:02:04,750
Now, if you have a very large
JavaScript application,

29
00:02:04,750 --> 00:02:10,020
it becomes cumbersome to write
the entire code in one single file.

30
00:02:10,020 --> 00:02:14,890
And obviously you want the results to
be able to break your application into

31
00:02:14,890 --> 00:02:16,830
multiple facts.

32
00:02:16,830 --> 00:02:21,760
Now, unlike traditional programming
languages, JavaScript never had a way

33
00:02:21,760 --> 00:02:26,945
of distributing the code into multiple
files and then combining them together.

34
00:02:26,945 --> 00:02:32,095
So this is where the CommonJS API
came in to fill in this

35
00:02:32,095 --> 00:02:35,859
gap that fills in the needs for
some common application.

36
00:02:35,859 --> 00:02:42,832
And this CommonJS format defines
a module format that can be used for

37
00:02:42,832 --> 00:02:47,282
breaking up your JavaScript
application into multiple files.

38
00:02:47,282 --> 00:02:51,151
And Node adopts that CommonJS format for

39
00:02:51,151 --> 00:02:55,349
organizing our JavaScript
application into multiple files.

40
00:02:55,349 --> 00:02:59,861
And within JavaScript,
with the CommonJS format,

41
00:02:59,861 --> 00:03:03,891
each file becomes its own Node module.

42
00:03:03,891 --> 00:03:09,841
So let's learn a little bit more about
Node modules in the rest of this lecture.

43
00:03:09,841 --> 00:03:11,931
So as I just mentioned,

44
00:03:11,931 --> 00:03:16,280
each file in a Node application
becomes its own Node module.

45
00:03:16,280 --> 00:03:22,290
So the file or the JavaScript file
defines the boundary for a Node module.

46
00:03:22,290 --> 00:03:26,160
So within that file,
the CommonJS specification

47
00:03:26,160 --> 00:03:30,770
provides a variable called the module
variable which is a JavaScript object.

48
00:03:30,770 --> 00:03:36,070
And this gives you access to the current
module definition within a file.

49
00:03:36,070 --> 00:03:41,020
And on this module object,
you have the module.exports

50
00:03:41,020 --> 00:03:45,120
property which determines
the export from the current module.

51
00:03:45,120 --> 00:03:49,010
So when you assign something to
the module.exports property,

52
00:03:49,010 --> 00:03:54,100
then that becomes the exported
value from the current module.

53
00:03:54,100 --> 00:03:59,070
So that when this module is
imported into another file

54
00:03:59,070 --> 00:04:03,130
of our Node application,
then whatever is exported from this module

55
00:04:03,130 --> 00:04:06,860
becomes available in
the second application.

56
00:04:06,860 --> 00:04:09,330
We'll look at an example in short while.

57
00:04:09,330 --> 00:04:13,080
When you need to import
a module into another module,

58
00:04:13,080 --> 00:04:17,700
this is where the require function
is used to import the module.

59
00:04:17,700 --> 00:04:23,220
So as we will notice in a short while,
the require function is used

60
00:04:23,220 --> 00:04:28,800
to import a Node module that is defined
in other file into the current file so

61
00:04:28,800 --> 00:04:32,790
that it can be used within
our node application.

62
00:04:33,800 --> 00:04:36,750
Node modules can be of three categories.

63
00:04:36,750 --> 00:04:41,842
We have file-based Node modules where we
define the Node module within a file,

64
00:04:41,842 --> 00:04:46,270
within our application and
we make use of it within our application.

65
00:04:46,270 --> 00:04:51,100
Then, we have core modules
that are already part of Node.

66
00:04:51,100 --> 00:04:55,610
The Node designers kept these core
modules intentionally small so

67
00:04:55,610 --> 00:04:57,970
that Node can be kept small.

68
00:04:57,970 --> 00:05:01,060
And also provide sufficient
functionality so

69
00:05:01,060 --> 00:05:05,310
that external module designers
can add in their own

70
00:05:05,310 --> 00:05:10,320
functionality that can be leveraged
when we developed Node applications.

71
00:05:10,320 --> 00:05:18,580
So the core modules include path,
file system, os, util and a few others.

72
00:05:18,580 --> 00:05:22,670
We will encounter some of them
as we go along in this course.

73
00:05:22,670 --> 00:05:25,010
Then we have external Node modules.

74
00:05:25,010 --> 00:05:29,650
These are third-party Node modules that
are developed by Node developers, and

75
00:05:29,650 --> 00:05:33,210
then made available through
the Node ecosystem.

76
00:05:33,210 --> 00:05:40,586
So these external Node modules can be
install within our system using NPM.

77
00:05:40,586 --> 00:05:45,363
So many times you would see
us using NPM install and

78
00:05:45,363 --> 00:05:50,937
the name of the Node module and
that will be included within

79
00:05:50,937 --> 00:05:57,010
our Node application in a folder
named node_modules folder.

80
00:05:57,010 --> 00:06:05,000
And we will encounter external Node
modules in a later lesson in this course.

81
00:06:05,000 --> 00:06:07,630
How do we make use of Node modules?

82
00:06:07,630 --> 00:06:13,720
When you need to use a Node module within
another Node file within your application,

83
00:06:13,720 --> 00:06:16,960
then you would use the require function.

84
00:06:16,960 --> 00:06:21,180
I briefly mentioned about the require
function in one of the previous

85
00:06:21,180 --> 00:06:22,440
slides there.

86
00:06:22,440 --> 00:06:26,420
The require function for
file-based Node modules,

87
00:06:26,420 --> 00:06:29,073
you will specify this as require and

88
00:06:29,073 --> 00:06:33,955
then specify the path to the file
which contains the Node module.

89
00:06:33,955 --> 00:06:39,560
So you would say require./,
the module name if the file exists

90
00:06:39,560 --> 00:06:45,590
in the current folder written
which your Node application exist.

91
00:06:45,590 --> 00:06:51,060
So this specify the relative path to
the file from the current location.

92
00:06:51,060 --> 00:06:54,247
And also for the core and
external modules,

93
00:06:54,247 --> 00:07:00,260
you would normally specify them by saying
require and the name of the module.

94
00:07:00,260 --> 00:07:03,090
You would explicitly specify a path for
it.

95
00:07:03,090 --> 00:07:06,200
If it is a core module,
it's already part of Node and so

96
00:07:06,200 --> 00:07:08,120
it will be automatically included.

97
00:07:08,120 --> 00:07:13,365
If it is an external module,
then it will be installed either within

98
00:07:13,365 --> 00:07:18,978
the node_modules folder in the current
folder, or if the Node doesn't

99
00:07:18,978 --> 00:07:25,327
find the external module within the
node_modules folder in the current folder,

100
00:07:25,327 --> 00:07:31,058
it will go up to the next higher level
folder looking for that Node module.

101
00:07:31,058 --> 00:07:33,989
Or the next higher level folder and

102
00:07:33,989 --> 00:07:39,143
up the hierarchy until it locates
the Node modules which will

103
00:07:39,143 --> 00:07:44,390
then be imported to be used
within your Node application.

104
00:07:44,390 --> 00:07:48,040
If it is unable to find the Node
module up the hierarchy,

105
00:07:48,040 --> 00:07:52,640
then it'll obviously raise an error
saying that the Node module is missing.

106
00:07:52,640 --> 00:07:57,690
This way of organizing is
very useful in the way

107
00:07:57,690 --> 00:08:03,220
the Node application structure
is defined as we will encounter

108
00:08:04,840 --> 00:08:08,460
more in the later part of this course.

109
00:08:08,460 --> 00:08:13,530
With this quick introduction to Node
modules, let's take a look at an example

110
00:08:13,530 --> 00:08:19,150
which we will do as part of
the exercise that follows this lecture.

111
00:08:19,150 --> 00:08:23,210
As we learned in this lecture, a Node

112
00:08:23,210 --> 00:08:28,040
module's boundary is defined by
the file that contains the code here.

113
00:08:28,040 --> 00:08:34,060
So here, I have an example of a Node
module defined as rectangle.js.

114
00:08:34,060 --> 00:08:38,113
And in there you can see that
I am using two exports here,

115
00:08:38,113 --> 00:08:42,096
I'm saying exports.perimeter and
exports.area.

116
00:08:42,096 --> 00:08:47,401
And you'll see that here I'm not
using module.exports because

117
00:08:47,401 --> 00:08:52,740
exports itself is also a shortened
version of module.exports.

118
00:08:52,740 --> 00:08:57,057
So if you don't want to use
module.exports completely but

119
00:08:57,057 --> 00:09:01,804
instead just want to exports,
then this is another way of writing

120
00:09:01,804 --> 00:09:06,316
those items that are exported
from the current Node modules.

121
00:09:06,316 --> 00:09:09,875
So here we are exporting two functions,
the perimeter and

122
00:09:09,875 --> 00:09:12,750
the area from the rectangle Node module.

123
00:09:12,750 --> 00:09:17,679
Now in order to make use of
this module in another file,

124
00:09:17,679 --> 00:09:21,870
then let's take a look
at the index.js file.

125
00:09:21,870 --> 00:09:27,240
And the very first line in this
file you will notice that it says

126
00:09:27,240 --> 00:09:32,220
var rect = require and
within quotes, ./rectangle.

127
00:09:32,220 --> 00:09:36,650
So this is specifying that the rectangle
module is going to be important in here,

128
00:09:36,650 --> 00:09:42,090
I made use of within this
particular file of the application.

129
00:09:42,090 --> 00:09:45,670
So this is how we would
define Node modules, and

130
00:09:45,670 --> 00:09:50,080
this is an example of
a file-based Node module.

131
00:09:50,080 --> 00:09:52,197
We will encounter the core and

132
00:09:52,197 --> 00:09:56,438
the external Node modules in
later lessons of this course.

133
00:09:56,438 --> 00:10:02,669
[MUSIC]