1 00:00:00,240 --> 00:00:06,300 In this video as promised you are going to get a bit of experience working with an NPM package on your 2 00:00:06,360 --> 00:00:06,900 own. 3 00:00:06,900 --> 00:00:12,500 Before we get into the challenge though I want to take a quick moment to talk about node modules. 4 00:00:12,510 --> 00:00:15,240 The directory that NPM created. 5 00:00:15,240 --> 00:00:17,100 This is a generated directory. 6 00:00:17,100 --> 00:00:19,700 So that means that NPM generates it. 7 00:00:19,710 --> 00:00:25,950 And it's not something we should be manually editing because when we use the NPM command again our edits 8 00:00:26,040 --> 00:00:27,990 are going to get over written. 9 00:00:28,050 --> 00:00:34,980 You can actually recreate this directory from scratch using NPM based off of the contents of package 10 00:00:35,030 --> 00:00:38,550 dot Jason and package lock dot Jason. 11 00:00:38,580 --> 00:00:45,600 So if you were to download a zip for this course or you were to grab the code from the get hub repository 12 00:00:45,600 --> 00:00:51,300 which contains all of the course code you'll notice that the node modules directory is nowhere to be 13 00:00:51,300 --> 00:00:52,020 found. 14 00:00:52,050 --> 00:00:56,310 So let's experiment with this by actually deleting it from our local project. 15 00:00:56,310 --> 00:01:02,400 I'm going to remove that folder and this is exactly what you would see if you grab the lecturer zip 16 00:01:02,580 --> 00:01:04,230 for the last lesson. 17 00:01:04,230 --> 00:01:08,160 It's the same files but with no node modules directory. 18 00:01:08,160 --> 00:01:14,550 Now the node modules directory is necessary for our app to run when we require an NPM library. 19 00:01:14,580 --> 00:01:19,200 It's going to look in that node modules folder for the application code. 20 00:01:19,200 --> 00:01:22,980 So if I were to run at J.S. Right now it would fail. 21 00:01:22,980 --> 00:01:24,400 Let's do it just to see. 22 00:01:24,540 --> 00:01:26,420 So node space app dot J. 23 00:01:26,430 --> 00:01:27,180 S. 24 00:01:27,210 --> 00:01:30,090 I'm gonna go ahead and run this command and what do we get. 25 00:01:30,090 --> 00:01:32,760 We get a nice error message right here. 26 00:01:32,760 --> 00:01:36,520 Error can not find module validator. 27 00:01:36,570 --> 00:01:40,720 So it's complaining that we're using this module but it's unable to locate it. 28 00:01:40,890 --> 00:01:45,150 So clearly we do need the contents of that node modules folder. 29 00:01:45,150 --> 00:01:47,610 The answer is how do we get it back. 30 00:01:47,610 --> 00:01:51,120 All we have to do is run npm install. 31 00:01:51,120 --> 00:01:54,360 We're not going to provide any specific packages afterwards. 32 00:01:54,360 --> 00:01:56,520 This is the exact command. 33 00:01:56,520 --> 00:02:03,090 When we do this it's going to look at the contents of package lock dot Jason and package dot Jason to 34 00:02:03,090 --> 00:02:07,890 determine which dependencies and which versions our application is using. 35 00:02:07,890 --> 00:02:13,890 It's then going to recreate that node modules folder from scratch based off of just the contents of 36 00:02:13,890 --> 00:02:15,870 these two text files. 37 00:02:15,870 --> 00:02:20,550 So for larger applications the node modules folder can get really really big. 38 00:02:20,580 --> 00:02:25,170 So you might want to delete it if you were to share the code with a friend or if you were to add it 39 00:02:25,170 --> 00:02:27,580 to your own GitHub repository. 40 00:02:27,660 --> 00:02:33,040 You can always get that folder back when you download the project by running npm install. 41 00:02:33,060 --> 00:02:39,060 So right here I'm going to run that command and it's going to regenerate node modules based off of the 42 00:02:39,060 --> 00:02:40,960 contents of our Jason files. 43 00:02:40,980 --> 00:02:45,720 Right here we can see that we still have the validator folder inside of there. 44 00:02:45,720 --> 00:02:52,220 After recreating the directory and once again our app is back to its working state node space app. 45 00:02:52,260 --> 00:02:52,540 J. 46 00:02:52,540 --> 00:02:56,700 S I run the script and we do indeed get the correct results. 47 00:02:56,700 --> 00:03:03,810 So when you download the code for this course make sure to run npm install from inside of the project 48 00:03:03,810 --> 00:03:06,740 you're working with before you try to run any of the scripts. 49 00:03:06,770 --> 00:03:11,110 Now let's jump into the core of the video which is going to be a challenge for you. 50 00:03:11,130 --> 00:03:16,530 You're going to install require and use a brand new NPM library. 51 00:03:16,530 --> 00:03:19,470 Let's go ahead and talk about which one you're going to use. 52 00:03:19,500 --> 00:03:26,190 I'm gonna head over to the NPM search bar the library you're going to use is called Chalk like sidewalk 53 00:03:26,190 --> 00:03:26,870 chalk. 54 00:03:26,940 --> 00:03:32,220 Right here we can pull up the package page and you can see that this one is even more popular than the 55 00:03:32,220 --> 00:03:34,790 validator package we used before. 56 00:03:34,800 --> 00:03:38,240 This one gets 15 million weekly downloads. 57 00:03:38,340 --> 00:03:43,080 It is a very popular utility for all sorts of note applications. 58 00:03:43,080 --> 00:03:45,240 Now what exactly does Chalke do. 59 00:03:45,240 --> 00:03:49,220 Well if we scroll down on the package page we can learn a bit more about it. 60 00:03:49,230 --> 00:03:55,110 Chalk is a little utility that allows us to customize how text gets printed to the console when working 61 00:03:55,110 --> 00:03:55,880 with node. 62 00:03:55,890 --> 00:03:59,280 So here for example this is the default styling with chalk. 63 00:03:59,280 --> 00:04:04,170 We can override that and that can be a very useful thing to be able to do. 64 00:04:04,170 --> 00:04:10,020 Imagine you're creating a web server using something like express and NPM package we'll be exploring 65 00:04:10,020 --> 00:04:11,310 later in the class. 66 00:04:11,310 --> 00:04:17,670 You might want to customize how you print logs for your server maybe you print errors in red or with 67 00:04:17,670 --> 00:04:23,720 a red background and maybe you print success messages in green while you print warnings in yellow. 68 00:04:23,820 --> 00:04:29,610 Using a simple tool like chalk you can create better applications that make it easier for people to 69 00:04:29,610 --> 00:04:31,800 figure out what exactly is going on. 70 00:04:32,190 --> 00:04:36,950 So it's going to be your job to install it and use it inside of our little app. 71 00:04:36,940 --> 00:04:43,350 Dot J s script over in Visual Studio code before we talk about what I'd like you to do. 72 00:04:43,380 --> 00:04:49,770 Let's remove our console dialogue call with our call to validator dot is U.R.L. and let's also remove 73 00:04:49,800 --> 00:04:55,410 that require on line one where we load the validator library in now down below. 74 00:04:55,440 --> 00:05:00,310 We're going to talk about the challenge for this one and right here I have all of the comments to show 75 00:05:00,310 --> 00:05:01,650 you how to get this done. 76 00:05:01,720 --> 00:05:03,120 So big picture goal. 77 00:05:03,130 --> 00:05:09,280 Use the chalk library in your project and that's going to require you to do a few different things. 78 00:05:09,280 --> 00:05:11,540 First up you're going to have to install it. 79 00:05:11,590 --> 00:05:16,210 You want to install a version to appoint four point one of chalk. 80 00:05:16,210 --> 00:05:17,960 Now if you forget the command. 81 00:05:17,980 --> 00:05:18,880 That's OK. 82 00:05:18,880 --> 00:05:21,340 That is the entire point of these challenges. 83 00:05:21,340 --> 00:05:22,960 It's to give you experience. 84 00:05:22,960 --> 00:05:24,870 So you build that muscle memory. 85 00:05:24,940 --> 00:05:27,850 You can refer to the previous video to look up that command. 86 00:05:27,850 --> 00:05:34,630 Or better yet crack open the PDA f guide as that is a very easy way to scan between lessons and figure 87 00:05:34,630 --> 00:05:36,160 out exactly what was covered. 88 00:05:36,190 --> 00:05:41,500 It includes code snippets as well as command line commands that we covered in the lesson. 89 00:05:41,500 --> 00:05:47,890 Step two you're going to load chalk in using require just like we did for validator. 90 00:05:47,890 --> 00:05:54,070 So that could happen up here on line one just before our require call to load and notes then you're 91 00:05:54,070 --> 00:06:02,410 going to use the chalk library to print the string success in the console with green text. 92 00:06:02,410 --> 00:06:07,960 So green text would look a bit like what we have over here though the exact shade of green will depend 93 00:06:07,960 --> 00:06:11,410 on your operating system and other display settings. 94 00:06:11,410 --> 00:06:14,640 Now once you have that done you're going to test your work. 95 00:06:14,710 --> 00:06:21,700 So you want to run our script after saving it and you should see the green text success down below in 96 00:06:21,760 --> 00:06:22,990 the terminal. 97 00:06:22,990 --> 00:06:27,940 Now if you're wondering how you're supposed to get this done considering you've never used the library 98 00:06:27,940 --> 00:06:28,710 before. 99 00:06:28,780 --> 00:06:31,120 That's where the documentation comes into play. 100 00:06:31,240 --> 00:06:35,690 Maybe you're saying Hey Andrew I bought the course so you could teach me these things. 101 00:06:35,800 --> 00:06:40,780 And well we're going to cover a ton of packages in detail learning exactly how they work and how you 102 00:06:40,780 --> 00:06:43,280 can build complex apps with them. 103 00:06:43,300 --> 00:06:48,580 The goal is to also give you a bit of experience here and they're learning how to use new tools on your 104 00:06:48,670 --> 00:06:49,330 own. 105 00:06:49,390 --> 00:06:54,940 And I'll show you once again how to actually solve the challenge afterwards it's important to build 106 00:06:54,940 --> 00:06:56,260 up this experience. 107 00:06:56,260 --> 00:07:00,880 So when you're done with a class you can go off and explore all of the interesting and awesome things 108 00:07:00,880 --> 00:07:03,950 that NPM has to offer down below. 109 00:07:04,000 --> 00:07:10,060 There is some example usages here they are printing some text in blue so you can use this as a starting 110 00:07:10,060 --> 00:07:10,670 point. 111 00:07:10,750 --> 00:07:16,240 And if you were to scroll down through the documentation there's a styles section which gives you even 112 00:07:16,240 --> 00:07:21,010 more insight onto the various modifiers and colors that are available to you. 113 00:07:21,460 --> 00:07:26,530 So take some time to really explore the docs and figure out how to solve this challenge when you're 114 00:07:26,530 --> 00:07:27,100 done with that. 115 00:07:27,100 --> 00:07:31,990 You can always use the bonus to experience a bit more of what chalk has to offer. 116 00:07:31,990 --> 00:07:38,740 Use the docs to mess around with other styles such as making the text bold C in here or inverting the 117 00:07:38,740 --> 00:07:41,680 text where the color becomes the background. 118 00:07:41,680 --> 00:07:43,860 So here that would be green inverse. 119 00:07:43,900 --> 00:07:47,920 As an example just take some time to mess around with a few other ways. 120 00:07:47,920 --> 00:07:50,070 Chalk can be used as always. 121 00:07:50,140 --> 00:07:52,900 We're going to work through all of the solutions together. 122 00:07:52,900 --> 00:07:57,280 So if you get stuck or you can't quite get something that is a ok. 123 00:07:57,400 --> 00:08:03,160 All right pause the video give this your best effort and when you're done come back and click play 124 00:08:07,100 --> 00:08:07,940 how'd you do. 125 00:08:07,940 --> 00:08:10,700 Let's go ahead and walk through these solution together. 126 00:08:10,700 --> 00:08:13,610 So step one we want to install this library. 127 00:08:13,610 --> 00:08:18,650 I already gave you the exact version number and the module name which means down below. 128 00:08:18,710 --> 00:08:24,770 From the notes app folder we can run npm install providing that package name. 129 00:08:24,770 --> 00:08:31,610 So right here we are installing a new library so we do indeed list it out shock like we did in the last 130 00:08:31,610 --> 00:08:32,470 video. 131 00:08:32,540 --> 00:08:37,760 Then we use the at sign to provide the version two point four point one. 132 00:08:37,760 --> 00:08:44,360 Now we can go ahead and run that command which is going to install chalk and all of its dependencies. 133 00:08:44,390 --> 00:08:49,670 Once we have that done we can move on to Step Two and actually load it in. 134 00:08:49,700 --> 00:08:53,900 Now when it comes to loading it in and using it this is where the docs come into play. 135 00:08:53,990 --> 00:08:57,380 Right here I can see that I can load it in and use it like so. 136 00:08:57,380 --> 00:09:02,210 So let's get that done up above on line one though it could have been below this line. 137 00:09:02,210 --> 00:09:08,930 I'm going to create my constant called chalk and I am going to load in the library by using require 138 00:09:09,200 --> 00:09:11,020 remember for NPM packages. 139 00:09:11,030 --> 00:09:16,320 We load it in by providing its name inside of the string we Pastor require. 140 00:09:16,460 --> 00:09:21,950 Now we have access to all of the utilities that chalk exports and we can go ahead and take advantage 141 00:09:21,950 --> 00:09:22,590 of them. 142 00:09:22,640 --> 00:09:27,870 And in this case our goal is to print the success message in green. 143 00:09:27,950 --> 00:09:29,780 So right here we're gonna get that done. 144 00:09:29,780 --> 00:09:34,310 And it all starts with a console dialogue call. 145 00:09:34,550 --> 00:09:35,540 What are we going to do. 146 00:09:35,540 --> 00:09:43,030 I'm going to call a another method inside of there that is chock dot followed by the color. 147 00:09:43,050 --> 00:09:48,900 So this is one of the things you have to discover for yourself right here they used chalk dot blue but 148 00:09:48,900 --> 00:09:50,920 I wanted you to print things in green. 149 00:09:51,030 --> 00:09:56,190 If you scroll down there are even more examples like chalk red which you may have seen and they even 150 00:09:56,190 --> 00:09:58,460 use chocked green right here. 151 00:09:58,470 --> 00:10:02,360 So all we have to do is use chalk dot green. 152 00:10:02,700 --> 00:10:03,660 That's a method. 153 00:10:03,660 --> 00:10:08,830 And we're gonna go ahead and provide our string which is indeed the string success. 154 00:10:08,940 --> 00:10:12,170 Let's go ahead and be successful at spelling success. 155 00:10:12,210 --> 00:10:17,550 Now once again remember you could have created a variable first and past that in that would have been 156 00:10:17,550 --> 00:10:22,350 a perfectly valid way to solve the challenge as well right here for example. 157 00:10:22,560 --> 00:10:28,980 I could have created a concept called green message setting an equal to whatever comes back from our 158 00:10:28,980 --> 00:10:31,200 call to chalk green. 159 00:10:31,310 --> 00:10:37,290 And then I could have passed green message in both of those would have been identical solutions in terms 160 00:10:37,290 --> 00:10:38,640 of functionality. 161 00:10:38,640 --> 00:10:41,040 Now let's go ahead and test our work. 162 00:10:41,040 --> 00:10:44,210 I'm gonna save app dot J S from the terminal. 163 00:10:44,220 --> 00:10:47,920 We need to rerun our script that is node space app. 164 00:10:48,010 --> 00:10:48,370 J. 165 00:10:48,370 --> 00:10:49,860 S and what do I get. 166 00:10:49,980 --> 00:10:53,430 I get green M S H is not defined. 167 00:10:53,430 --> 00:10:55,890 So here I have a small typo. 168 00:10:55,920 --> 00:10:59,370 Clearly I meant to reference the green message variable. 169 00:10:59,370 --> 00:11:06,590 So all I need to do is replace M S H with M S G matching up with the variable name up above. 170 00:11:06,600 --> 00:11:11,610 Now I could have stopped the recording and gone back and refilled it pretending to be perfect but that's 171 00:11:11,610 --> 00:11:12,660 not the point. 172 00:11:12,660 --> 00:11:18,570 The point is to actually solve problems and work through our issues becoming better developers along 173 00:11:18,570 --> 00:11:19,200 the way. 174 00:11:19,200 --> 00:11:22,820 So every once in a while sure I'll leave my errors in. 175 00:11:22,920 --> 00:11:25,590 Now let's go ahead and save the program again. 176 00:11:25,620 --> 00:11:31,590 After typing the variable name incorrectly I'm going to rerun the script using the up arrow key and 177 00:11:31,650 --> 00:11:39,360 enter and here I am indeed getting my success message printing in green which is a fantastic step in 178 00:11:39,360 --> 00:11:40,370 the right direction. 179 00:11:40,380 --> 00:11:44,700 Now when it comes to the bonus there really is no right or wrong way to solve that. 180 00:11:44,700 --> 00:11:51,090 The goal was to just mess around with another styling option or two and if you head over to the example 181 00:11:51,090 --> 00:11:55,850 of documentation you can see that there is a line where they chain things together. 182 00:11:55,950 --> 00:12:04,230 Now here they call chalk dot blue dot B G red dot bold to create text that's bold on a red background 183 00:12:04,260 --> 00:12:10,680 where the text itself is blue so we can go ahead and use bold with our current setup to create bold 184 00:12:10,740 --> 00:12:12,350 green text. 185 00:12:12,420 --> 00:12:14,640 Now the order you put things in doesn't matter. 186 00:12:14,670 --> 00:12:22,320 So it could be bold first then green or Green first then Bolt let's prove it choc dot bold dot Green 187 00:12:22,770 --> 00:12:28,170 I'm gonna save the program I'm going to rerun things the program works and we do get our bold green 188 00:12:28,200 --> 00:12:33,810 text Now let's remove bold and one of the extra dots and put it after Green. 189 00:12:33,810 --> 00:12:41,910 So here I'm going to call shock dot Green Dot bold calling bold as a method if we call bold as a method 190 00:12:41,970 --> 00:12:45,230 and rerun the script we get the exact same result. 191 00:12:45,240 --> 00:12:51,270 So here we can chain together exactly what we need to create any sort of styling we want giving us a 192 00:12:51,330 --> 00:12:58,200 nice set of options we could add a third style into the mix alongside of green and bold. 193 00:12:58,200 --> 00:13:00,530 Now maybe you tried to use inverse. 194 00:13:00,630 --> 00:13:05,940 If you scroll through this example which is a great way to figure out how to use a library you'll notice 195 00:13:05,940 --> 00:13:08,530 that inverse doesn't actually exist here. 196 00:13:08,550 --> 00:13:12,960 They use things like underline they use underline there and down below. 197 00:13:12,960 --> 00:13:16,280 But there's nothing related to inverse seeing the colors. 198 00:13:16,380 --> 00:13:20,470 If we scroll down a bit though we can actually pull up the API docs. 199 00:13:20,610 --> 00:13:27,060 If we go back down to that Styles section we can see all of these style modifiers and all of the colors 200 00:13:27,060 --> 00:13:28,320 available to us. 201 00:13:28,320 --> 00:13:31,450 Right here there's another option called inverse. 202 00:13:31,470 --> 00:13:34,320 Let's go ahead and actually use it just to see what happens. 203 00:13:34,320 --> 00:13:38,480 Now once again I could add inverse first in the middle or at the end. 204 00:13:38,490 --> 00:13:40,280 I'm just going to toss it right here. 205 00:13:40,350 --> 00:13:45,980 So we have chalk dot Green Dot inverse dot Bolt. 206 00:13:46,020 --> 00:13:49,370 All this is going to do is make the green color the background. 207 00:13:49,380 --> 00:13:51,810 And if I run the script that's exactly what we got. 208 00:13:52,170 --> 00:13:53,010 So there we go. 209 00:13:53,010 --> 00:13:55,500 We have completed the challenge successfully. 210 00:13:55,500 --> 00:14:02,190 I'm going to remove those challenge comments and we now know how to use NPM libraries in our project 211 00:14:02,430 --> 00:14:08,040 which is a good thing because we're going to use NPM libraries throughout the course to create our back 212 00:14:08,070 --> 00:14:12,050 end API as real time apps and more. 213 00:14:12,060 --> 00:14:17,950 Now there's just one more way we're going to explore how we can install and use NPM modules and it's 214 00:14:17,950 --> 00:14:20,420 going to make our lives a lot easier. 215 00:14:20,430 --> 00:14:23,340 So let's go ahead and jump right in to the next video.