1 00:00:00,300 --> 00:00:06,600 I've got some exciting news for you and know the exciting news isn't that I'm now using Vrsaljko code 2 00:00:06,600 --> 00:00:10,470 instead of sublime text, I still love my sublime text code editor. 3 00:00:10,620 --> 00:00:17,550 But just to change things up, I'm going to use this code for now and I'm going to talk about ESX modules 4 00:00:17,850 --> 00:00:19,230 in note. 5 00:00:19,950 --> 00:00:26,730 You see, up until now, we had to use the required syntax, which is what we call the common JS syntax 6 00:00:26,730 --> 00:00:27,300 with note. 7 00:00:27,790 --> 00:00:35,550 Originally node was created with this syntax in mind because JavaScript wasn't created with modules 8 00:00:35,550 --> 00:00:40,440 in mind and they never had the import export syntax that we had with S6. 9 00:00:40,590 --> 00:00:47,730 So because Node was created before we even had that, they had to create their own module system, which 10 00:00:47,730 --> 00:00:51,180 is how we got require and this common syntax. 11 00:00:51,750 --> 00:00:59,790 But as of node version 12, they now have S6 style module imports and exports. 12 00:01:00,630 --> 00:01:06,420 So the good news is that if you're watching this video and you have the latest version of Node, then 13 00:01:06,420 --> 00:01:14,200 congratulations, you can now start to change from require to using import export keywords. 14 00:01:14,730 --> 00:01:20,580 So let me show you, based on what we've just done and what we've learned about modules, how we can 15 00:01:20,580 --> 00:01:23,490 convert this to S6. 16 00:01:24,300 --> 00:01:30,780 So the first thing we can do with script to dogs is to well, actually you can remove this entire thing 17 00:01:31,350 --> 00:01:33,000 and we can just simply say. 18 00:01:35,760 --> 00:01:40,170 Consed large number, so now this gets exported. 19 00:01:41,150 --> 00:01:48,860 So I can see that next in here instead of the required syntax, let's comment that out. 20 00:01:49,770 --> 00:01:51,240 And let's say import. 21 00:01:53,330 --> 00:02:00,460 And in here, we'll import doing the structuring the large number that we export. 22 00:02:00,530 --> 00:02:08,750 So remember, we did export large number and we're going to say from a script to grab a large number. 23 00:02:09,650 --> 00:02:15,050 So this object, this structuring simply says out of the script to object that we're going to import, 24 00:02:15,260 --> 00:02:22,310 just grab the property, large number so I can say from an in here dot script to. 25 00:02:23,400 --> 00:02:27,240 Dodgiest, so now that I've imported large number. 26 00:02:28,360 --> 00:02:33,480 And I did object to structuring I don't need to call script to dot large number. 27 00:02:33,530 --> 00:02:36,780 Instead, I can just simply do large number. 28 00:02:37,420 --> 00:02:39,390 So will this work? 29 00:02:39,970 --> 00:02:40,950 Well, let's test it out. 30 00:02:41,420 --> 00:02:43,390 I'm going to open up the terminal here. 31 00:02:44,920 --> 00:02:52,060 And in here, I got to make sure I'm in the right folder so I should cwd into the S6 folder and in here 32 00:02:52,060 --> 00:02:54,830 I have the script and script to us. 33 00:02:56,350 --> 00:03:00,910 Now, if I do this, do you think it will work? 34 00:03:02,200 --> 00:03:04,820 Well, the first thing I want to do is check my note first. 35 00:03:04,930 --> 00:03:12,710 So if I do Noad Dashkova and you should do the same, what's the number that you get in here? 36 00:03:12,970 --> 00:03:16,180 I see that I have a pretty high note version. 37 00:03:16,420 --> 00:03:18,790 As a matter of fact, it's higher than version 12. 38 00:03:18,820 --> 00:03:23,430 And if you remember, with version 12, we got ESX imports. 39 00:03:23,440 --> 00:03:25,770 So this should work, right? 40 00:03:26,730 --> 00:03:27,640 Well, let's test it out. 41 00:03:28,020 --> 00:03:31,050 I'm going to say Noad script dot James. 42 00:03:33,700 --> 00:03:38,320 Syntax error cannot use a statement outside a module. 43 00:03:40,230 --> 00:03:41,640 Hmmm, why is that? 44 00:03:42,150 --> 00:03:43,500 I mean, I have the right version. 45 00:03:44,310 --> 00:03:51,660 Luckily, Noad gives us some hints warning to Loden ESX module set type module in Pakistan. 46 00:03:51,660 --> 00:03:54,980 Jason or use the MJF extension. 47 00:03:55,710 --> 00:03:57,960 And this is a little Gocha. 48 00:03:59,570 --> 00:04:07,760 Because node was created with this common import export in mind, we need to actually specify. 49 00:04:08,970 --> 00:04:16,320 How we want know to read these files, so we know that we're using the ESX syntax because remember, 50 00:04:16,320 --> 00:04:17,970 it's different from a browser. 51 00:04:18,420 --> 00:04:20,970 So we have two options here. 52 00:04:21,120 --> 00:04:26,570 And I also want to show you how you can find this out on the documentation website for note. 53 00:04:27,030 --> 00:04:31,800 Usually what you would do is Google ECMAScript six nogs. 54 00:04:31,920 --> 00:04:35,160 You'll probably get to the nogs documentation. 55 00:04:35,370 --> 00:04:40,680 And we can see here we have module's ECMAScript modules and let's go to enabling. 56 00:04:41,590 --> 00:04:44,050 And if we click on enabling, we see over here. 57 00:04:44,620 --> 00:04:45,500 All right, there you go. 58 00:04:45,550 --> 00:04:50,250 We need to add the MDGs file extension inside of package. 59 00:04:50,440 --> 00:04:53,230 Jason, we have to update the field. 60 00:04:53,920 --> 00:05:00,310 So if I click on the type field, I'll read about it here and I say, oh, in poor segments of JS files 61 00:05:00,310 --> 00:05:06,100 are treated as S modules if the nearest parent package that JSON contains type module. 62 00:05:06,850 --> 00:05:10,000 So this is a good way to learn how to read the documentation. 63 00:05:10,030 --> 00:05:14,260 Remember, the answer is probably always inside of the documentation. 64 00:05:15,010 --> 00:05:16,870 So let's follow their advice. 65 00:05:17,170 --> 00:05:19,630 What if I rename these? 66 00:05:23,330 --> 00:05:23,970 M. J. 67 00:05:24,050 --> 00:05:26,360 Yes, and this to. 68 00:05:30,460 --> 00:05:31,900 If I now run this. 69 00:05:33,980 --> 00:05:35,050 Do you think this will work? 70 00:05:36,700 --> 00:05:39,000 Actually, I have to fix one more thing, right? 71 00:05:39,340 --> 00:05:43,840 I have to make sure that the syntax while we're importing is M James. 72 00:05:45,010 --> 00:05:49,030 For fun, let's see if I can do it without the file extension. 73 00:05:49,060 --> 00:05:51,250 Let's save here and run this. 74 00:05:57,140 --> 00:06:07,700 Cannot find module, so we actually have to specify the file extension and if I run this again. 75 00:06:09,600 --> 00:06:16,020 Hey, check that out, everything works out, Essex Imports works, and we just followed what they've 76 00:06:16,020 --> 00:06:22,590 recommended name false IMGs, but there was another option to write the package JSON option. 77 00:06:23,070 --> 00:06:31,590 So let's rename this back to some people really don't like the syntax IMGs and they prefer this method 78 00:06:31,590 --> 00:06:32,080 instead. 79 00:06:32,880 --> 00:06:35,490 So, again, what we'll do. 80 00:06:36,650 --> 00:06:46,040 Is will run this again and make sure we change this to dogs, back to dogs, and if we run our code, 81 00:06:46,760 --> 00:06:53,960 you'll give us an error because again, we have to add type module inside of package that Jason. 82 00:06:54,260 --> 00:06:54,610 Hmm. 83 00:06:55,490 --> 00:06:57,530 We don't really have packaged Jason. 84 00:06:57,530 --> 00:06:57,820 Right. 85 00:06:58,100 --> 00:07:06,500 But we can just run the command npm in it and then dash y to say yes to all the options to simply create 86 00:07:06,710 --> 00:07:07,550 our package. 87 00:07:07,920 --> 00:07:08,330 Jason? 88 00:07:08,360 --> 00:07:09,440 Well, there you go. 89 00:07:10,880 --> 00:07:18,110 So in here, all we need to do is just simply inside of package that Jason wherever we want ad type 90 00:07:18,800 --> 00:07:25,220 module so that it reads the package that Jason and says, oh, yeah, they're going to be using ESX 91 00:07:25,220 --> 00:07:25,660 modules. 92 00:07:26,330 --> 00:07:31,490 So if I see this and now go back and run my code. 93 00:07:33,620 --> 00:07:36,010 Look at that, it all works. 94 00:07:36,250 --> 00:07:37,120 How cool is that? 95 00:07:38,120 --> 00:07:46,060 Now, go ahead and play around with ESX modules as you start working with more and more NOAD code, 96 00:07:46,370 --> 00:07:51,740 you're going to see more import statements than require statements like we have before. 97 00:07:52,250 --> 00:07:58,150 However, because a lot of code bases still use the required syntax. 98 00:07:58,250 --> 00:08:04,550 If you go on NPM, for example, and try to download packages, a lot of them still suggest that you 99 00:08:04,550 --> 00:08:11,540 use the require syntax, and that is because you're not sure that everybody has the latest version of 100 00:08:11,540 --> 00:08:11,840 note. 101 00:08:12,050 --> 00:08:17,190 Some people might be running a node version that is lower than version 12. 102 00:08:17,420 --> 00:08:19,880 In that case, the code isn't going to work. 103 00:08:20,390 --> 00:08:26,300 And that's why for the rest of the course and even for the final Smart Brain project, I've left our 104 00:08:26,300 --> 00:08:33,500 note files with the required syntax so that anybody, any student taking this course can run those without 105 00:08:33,500 --> 00:08:34,220 any errors. 106 00:08:34,610 --> 00:08:39,440 But feel free to update them if you ever want with S6 imports. 107 00:08:39,830 --> 00:08:46,130 But for now, it's always a good idea to keep the [REMOVED]'s syntax to make sure that if your code is 108 00:08:46,130 --> 00:08:52,220 being run on different systems and you don't have control over the node version, well, then you can 109 00:08:52,220 --> 00:08:56,160 make sure that they require syntax is going to work for everybody. 110 00:08:57,050 --> 00:08:58,640 I'll see in the next one, pabai.