Mastering Node.js Modules by Jeremy Foster at Silicon Valley Code Camp

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right I'm Jeremy Foster I'm a developer evangelist at Microsoft it says that they're gonna be two presenters today my colleague gabby is woke up sick so I think I can handle this but Gabby's great she's new to our team and she spent a whole lot of time coming up to speed on this and she was all ready to go in to choose sick this morning so if you see Gabby over by the Microsoft booth or something give her a big aah sorry what's that yeah right okay so we're gonna be talking about mastering nodejs modules and packages what this really is is a sort of an entry point into learning nodejs what I've come to discover is that when people go to learn nodejs they they use one of the there are a number of really good resources online learn you node code school there are a lot of courses on Pluralsight and one of the first things that you learn is like how to make a simple web server and how to do this and how to do that and a lot of times people save this topic of modules and packages until later to learn and so this whole beginning learning phase when you're learning nodejs you're doing these things called requires you're like pulling in these modules and packages and you haven't really learned how they work behind the scenes and so if if you've learned a little bit of node but you've never really dived into the topic of modules and packages this will be really beneficial and if you're absolutely brand-new to it this will be a really good first thing to learn about node because then when you go to build a web server and they say now go ahead and import the HTTP module you'll be you'll say I know exactly what's going on there so yeah yeah no js' no it stands for nothing yeah somebody said hey what's a good name for my new runtime my new my new engine for running JavaScript I'll call it a node and he just made that up no js' if you're gonna make something in JavaScript you have to come up with a silly name that's a that's a law it means nothing and a node is a name for a server you like if I'm gonna create a cluster in the cloud a farm of servers to do some work for me I call each of the individual servers a node and so he might be alluding to the fact that now we have the ability to run JavaScript on a server does that make sense and it's just another runtime there are lots of runtimes out there we've got dotnet Ruby Python all these all these different things there's different ways to run a certain kind of code and node is a way to run JavaScript code it is JavaScript running on the server but let me clear up a myth for you no js' is not JavaScript it's a C++ engine that runs JavaScript for you okay so it's a collection of c c++ libraries at the heart of them is one called libuv it's an open source cross-platform c library that you can run on almost any device in the world practically and it is capable of saying you give me a javascript file I'm using the chromium v8 engine I can execute that JavaScript for you I know I'm not a browser but that's okay I can execute that JavaScript for you that make sense good yeah so the if you if you have absolutely no idea what note is it's a little bit outside of the scope of this but I think some things you know I think that everything will still be will still be clear to you in the topic of modules and packages okay okay so this is Gabbie this is me I already told you who I am I should note that I'm spending a lot of time in in Azure these days because it's fun to do things in the cloud and I'm spending a lot of time with IOT this is a fun topic I don't I don't really want to leave the subject of node when I enter IOT I want to write node in IOT so my session at the end of yesterday was using nodejs on iot devices and it's surprising a lot of them will support node and I have what I call kind of the node threshold of glory if I have a device and I can get it up to node then all of a sudden I have the tremendous advantage of about a hundred and ninety or so thousand packages in the node online repository in the NPM repository online a hundred and ninety thousand packages almost anything you want to do it's already been written note unlike some of the other frameworks is a really light framework not a lot it doesn't do a lot out of the box on its own kind of like JavaScript you know it looks like you're like where's my date time functions oh there's a good library for that why would I have to go to another library for that shouldn't that be built in well some would say no it shouldn't be built in because if I pull it in from another library now I've opened it up for competition maybe somebody else will make a better one and I can replace it with that and now I don't have this overhead of having all of this you know these date time things built into to my core framework that I have to spin up I have to have this run time so I think it's a really good way to do things you guys can always find me online and I have a podcast where I talk a lot about nodi things Webby Things IOT things it's called code chat just go to code foster calm slash coach at I actually interviewed a guy from NPM it's gonna be showing here in two or three weeks and it was a it was a good chat about a guy that actually works at NPM it's pretty fun too it was pretty fun to talk to him about their company NPM is actually a private company that spun off of the node project a long time ago and they're just rocking it they're really cool ok so we're we're gonna assume that you've got maybe some basics in JavaScript if you don't you'll still be fine don't worry about it I'm not really gonna assume any node knowledge whatsoever that said I'm also not gonna cover it like I mentioned already I'm not really gonna cover the traditional basics of node I'm gonna jump right in on what our modules what our packages and if you have questions about the basics of node please feel free to still ask and and I should be able to answer those but I'm just gonna be real responsive on those subjects and yeah well because it I didn't say that no jeaious has nothing to do with JavaScript I said that it's an engine that's capable of running JavaScript and so all the things that we write in nodejs our JavaScript it's just that the engine itself is not in JavaScript some people say no GS is inherently slower because it's JavaScript no it's not it's a C very it's very very fast right it's just that your stuff is JavaScript so what we're gonna cover today is modules packages and then publishing packages okay really simple scope so it'll be nice cuz we'll be able to spend some time on the definition of a module the differentiation and definition of a package and then how you publish things there's a reason why there are something like a hundred and ninety thousand packages in the NPM repository online and it's because it's so easy to publish a package raise your hand if you have published if you have packaged a created and nougat package okay this is about two thousand times easier something like that roughly two thousand times easier so we'll see that in just a second okay so I'm gonna jump right out to node NP MJS org and and scroll down a little bit and just show you what I'm talking about with this online repository look at that a hundred and ninety one thousand total packages just a few months ago I was recording a course for Pluralsight called nodejs and the Internet of Things using Intel Edison really fun course you guys if you're Pluralsight subscribers you should check it out and when I was recording that it was just a few months ago I remember this was 130,000 so this is skyrocketing I mean the this it's like up by 10,000 every time I go to it so this is where you go to get the code that you know if you're trying to do facial recognition you don't write that you go look in here and somebody else has already written it for you if you're looking for a really good date/time parser you don't write that you go find one in here by the way it's called a moment it's a really good one all right so let's look at our first topic which is the fact that what we're doing here as developers the reason why you guys are coming to code camp is because you're trying to be awesome in software development right you guys don't just want to be standard you want to be awesome and there's awesome in every category life and I highly encourage you to constantly be seeking more knowledge constantly be venturing into other topics if you're a complete Java guy try dot net if you're a complete dotnet guy try javascript if you've never tried Ruby try Ruby you know like look at these different languages and explore and learn there's awesome in every category I'm gonna show you guys some awesome in some various categories okay there's awesome in beards okay there's awesome in that category as opposed to not awesome beards there's awesome in t-shirts this guy's obviously sad wrong position there's awesome in movie-going experiences awesome in airport transportation awesome my memory awesome security right there's awesome security and we want to be awesome awesome daycare how much that one move and and that's what we're striving to do we're striving to be awesome in our category we are software developers and that's what we're looking to do so okay now on to our first topic so our first topic is modules what is a nodejs module now I know that module is a very overloaded term we have Hardware modules you've got modules defined in almost every programming language it means something different it means something in nodejs and that's what I'm going to describe for you it's very generic though it's just a piece of reusable code it's kind of like classes and other languages right you encapsulate things with classes and you go this is a thing and it has the ability to it's a it's an animal and it has these descriptors like how many legs it has and it has these actions like whether or not it can run or jump or whatever and we get into inheritance it's a way of encapsulating code now classes are also a way of facilitating instantiation of objects right so you can create object-oriented scenarios modules are not by default facilitators of instantiation you can create modules that act like classes where you do instantiate them but by default that's not what they are now there are three kinds of modules there are core modules those are the ones that like when you install node you get those okay they're built right into the node engine you can't not have them and like I said they're quite light it if anybody looked through the object browser and the dotnet framework it's extremely rich right and it's nice because you take this dependency on the.net framework and then man you have all this functionality and you if you assume that your users have the dotnet framework then when you deliver an assembly it's minuscule because practically all the code is already on their computer in the form of the.net framework right well node works a bit differently what they get with node is not very much and then everything's delivered in the forms of these modules that come with your application it's just a different way of doing things I think both are great so there's core modules and then there are third-party modules this is the one that Frank wrote and I'm gonna use he published it online and I'm gonna download it and use it in my app I'm gonna I'm gonna take a dependency on Frank's code I'd trust him I've I've looked into it and he's got seventeen thousand stars on on NPM and people are using this it's a thing and it's gonna be maintained and he just worked on it last week so I trust it and so I'm gonna take a dependency on his library and I'm gonna use his third-party module and then there are local modules that's one where you go you know what I need to organize my code and pull this code out of my main file into something separate but I don't expect anybody else in the world cares about this because it's domain-specific it's only for my company it's only for what I'm doing so I'm gonna write it as my own module we call that a local module or a custom module okay follow me so far everybody any questions okay yeah all modules it there's there's a strange answer that question all modules have to be JavaScript but modules can also basically wrap what we call native add-ins so you can write it in C C++ or whatever but it's always kind of surfaced in JavaScript and you can see you're consuming it just like any other module just like this little JavaScript module but behind the scenes it can be an add-in that you write and whatever and when it's an add-in it's when it's a native module then it's inherently platform-specific right so it's possible for me to you'll find that when you download modules they'll say this is a pure JavaScript module that means it doesn't matter what platform you're on because it's just JavaScript and and those are really handy and they probably make up over 90% of the modules in the store that said the vast majority of them are pure JavaScript which is really nice but the native modules we call them are the ones that are sitting on top of a c add-in when you install those you'll see it does a whole bunch of stuff you'll see no chip and Python and and everything because it's trying to install that native module on your device it's looking and saying you've got Windows there you go you've got Linux there you go you've got Mac there you go and it installs the one that it needs for the architecture that you're running sometimes people create a module for nodejs and they ignore say the Windows platform and then if you're running Windows it's annoying because you try to you try to install it and it says there was an error I was expecting a forward slash and I got a back slash in a date it didn't work and that's annoying but it luckily that's a pretty minor percentage of them okay so when you bring these modules in you use this require keyword if you if you do an absolute like beginner course and in in nodejs almost the first line on the first file that you write is gonna be a require okay so I want you to know what exactly required does the first thing required does is it looks to see if this is a core module we're gonna type this in just a second so if you type require HTTP that's one of the core modules it's done it's like I just checked it that's a core module that's where I'm going to get so it's gonna get it from the core modules then it looks to see if it's not a core module it says well did you put a a path indicator on the beginning of this module name so did you do dot slash my custom module if you did then it knows that that is a local module so first it looks to see if it's a core module then it looks to see if it's a local module and then if it doesn't find that then it assumes that it's a third-party module it looks in this special folder here the node modules this will be abundantly clear when we do the demo but it looks in this node modules folder and tries to find it if it doesn't find it it says I couldn't find that module I looked in the core modules I looked to see if you indicated that it was a local module and I looked in node modules and I can't find it well that might just mean you need to install it if it's a third-party module you have to install it and that's extremely easy and by the way just so you know it's gonna work up this path all the way up to the root of your drive and keep looking at node module folders to see if it can find it so it creates this recursive architecture where you can have a dependency you can have a module that's buried and it's gonna climb the tree and find that package and use it anyway so pretty neat the way that it works it also looks in what we call the global path the the node path environment variable there's a place on your computer if you are in Linux then it's in wack route back home or home route rather if you're on Windows it's under users your user name app data roaming NPM that's the default for its kind of done for you and what that is is a collection of modules that you want to have on your computer that are not necessarily for a given project maybe they're just nice command-line utilities that you like to use and so you want it to be available everywhere you want it to not care what folder you happen to be in on your command line you want it to be available everywhere now this is a pretty cursory overview but if you want to get real in-depth information about exactly what I'm talking about here the definition of a module and how require looks for it then you can go to this in the official box yeah that's correct that's correct the question is if I create a module and I have it in my known modules folder and it's called charlie and then at some point in time the node foundation decides to create a core module called Charlie that's going to shadow your charlie module now those don't change that much so I the likelihood of that happening is extremely low but if in fact they created a core module that conflicted with yours then it would be on you to change the name of yours because you can't conflict with the core module names otherwise yours will be hidden that's correct yeah so the question is if you go to a file you're gonna be walking up the path I think what you mean is if you go if you specify a third-party module it's gonna walk up the path looking for it in node modules folders the global is only one folder however on your on your system so there's no walking to be done there just looks in what it's configured as the global store of modules and if it finds it great and if not it's gonna throw an error because that was the last place that it looked so it didn't find it so it's gonna throw an error okay so let's look at consuming a core module we're gonna jump over to the command line by the way I'll say this quite quickly but if you are in if you have not installed node yet on your system if you're in Linux you just do apt-get in install node if you're in Ubuntu or Debian it's a little bit strange for some reason the package in the package manager in apt-get store is called nodejs and you should also install nodejs - legacy so that all that that's a very simple one that basically just makes it so that the node command works for you so that's a little bit weird if you're in Windows you can use something like we were talking earlier about one get or chocolaty to install it but I find it easier to just go to node.js org and can anybody tell me how I would download this yeah they make it pretty clear the large green button that says download for Windows and that installs both node and NPM okay so that's the way to install that so I'm gonna jump out to a command line and I'm in my junk folder I'm using a shell of shells called commander that I really like you can see on the bottom that I have a tab called PowerShell dot exe I'm actually in PowerShell but I'm doing it in commander and it gives me some fun features it gives me a whole ton of properties and settings you can see that I'm slightly transparent you can see my slide behind this PowerShell does that as well as of Windows 10 but it also gives me the ability to like zoom with control wheel and all kinds of things so I really like this program so what I'm gonna do is I'm gonna make a new directory and I'm just gonna call this my first node app CD into that and now I have nothing in there right if you're a Windows guy if you're a DOS guy I have nothing in there so I'm gonna create my first node app by just simply creating a JavaScript file I'm gonna create the file by saying touch index dot J s much like the default document in a web project is either default dot HTML or index dot HTML the default document for node is index.jsp police app dot j s or sort of server j s index is the best one to use in my opinion because node inherently looks for that as the default so I'm gonna go ahead and create this file now I have index J s but it doesn't have anything inside of it now I'm gonna use visual studio code which I love being able to launch this from the command line and I can either edit that file directly or I can just say code this directory and I'm basically saying this is my project it's a folder but it's a project I don't have to have an explicit project file which is nice and it's gonna launch Visual Studio code it always it always launches in like one second which is my favorite thing about it and here I have this project in context and so I can click on index j s I'm gonna go ahead and drag that project browser all the way over and Here I am inside my first node out now I'm gonna require a core module I'm gonna pull in a core module I'm gonna save our HTTP I could make this whatever I want this is just my own variable equals require HTTP there we go so I've pulled in the HTTP module and if I save this jump back over to my command line obviously if I cat my index J s you can see that it has a line in it that's great and now to run this I just do node and I could either say node index J s or node is smart enough to say index J s is the default so if I just say node this directory it says hey I know what to do I'll look for an index J s and then find it now it ran it ran successfully it just didn't do anything right all I said was require this thing but it worked versus if I would have misspelled this it would have said I can't find module whatever okay so let's go ahead and go back to code let's say console.log HTTP what do you suppose that's gonna do it's gonna serialize that HTTP object and spit it out on the screen right there it is that's the HTTP object it's really nice working in JavaScript because when it's the language of the web and so when we go get anything from the web we almost always have a JSON object and you're you're ready to work with it there's no you know translation serialization mapping all that stuff you just you've got it and when you want to send something to the web you just take your local object make sure that it conforms to what they're expecting and you just send it to them so it's really nice full stack working in JavaScript I have a project I talked about yesterday about a nodejs on devices project called tweet monkey where I use nodejs to talk to it the Twitter streaming API and all my code is like this big it's like one you can almost fit it on one screen depending on your resolution on this resolution it would be two screens but it's really short and yet it's going to Twitter using OAuth to authenticate talking to the Twitter streaming API it's hooking up to it's what's called a stream in Twitter where I'm subscribing to a search and then whenever that search happens whenever Twitter finds a tweet with a certain search result it comes back to me and calls this function and that whole process is extremely easy well tweet monkey has an older brother that I didn't mention called command monkey and with command monkey I can take my Microsoft band and say monkey dance and he dances he does his little his little animation the way that works is the band goes to my phone goes to a service and azure goes down to the device and the fun thing about that project is that the whole thing is JavaScript JavaScript on the phone javascript in the cloud JavaScript on the device and when you're in JavaScript the whole way everything's really short and really easy I really love that space yeah all servers for the most part yes I'm sure they're edge cases but they're impractical almost every almost every server can run JavaScript to the point where almost every many many small devices can run JavaScript I mean we're talking about you raspberry pies no problem didn't tell edison's the size of an SD card no problem so a lot of devices can run JavaScript and that's that node threshold of glory I mentioned if it can run nodejs then all of a sudden this with this world of ease opens up to you which is really nice okay so that was consuming a core module very easy now let's consume a third-party module conceptually as far as your code is concerned it's very much the same I'm gonna leave that one there and now I'm gonna save our moment this happens to be something that somebody some very smart developer wrote it's a really good parser for dates and times it allows you to do things like Radio check that works is this gonna work temporarily okay cool okay well I'll keep rolling though so if I want to parse a date and time that something like 9/1 slash 2015 you've all you've all run across good date/time parsers that do that but how about if I want to parse something that's like two days from a week from tomorrow or the day after tomorrow or in two hours you know like written in written in human language you know not just numbers and slashes and dashes and stuff like that they've got really cool libraries like that so if you want to pull in moment all of a sudden in your app you're able to give somebody a text field where they enter next Thursday and it you know exactly what date that is how about two hours from next Thursday or two days from next Thursday or whatever things like that can open up so let's go ahead and use that one I'll say require I'll actually spell it right - that'll help and now let's comment that out for now let's jump over here and can anybody tell me what's gonna happen when I know this now yeah error I can't find this module moment I had no trouble finding HTTP because it's built-in but I can't find moment so what do I have to do I have to install it that's where NPM comes in there's the synergy synergistic relationship between node and NPM node is the engine NPM is the package manager handles all things modules and packages so it's critical to our subject today so I say NPM install moment I have to specify that moment is the package that I want to install and notice that I'm using the word package here we've gotten to the packages section technically what I'm installing is not a module it is a package '''l and I'll be really clear about the differentiation on that in just a second so I'm gonna go ahead and install that it's now going to the interwebs yes sir that's right I could either install it globally or locally and it would work but if I installed it globally then it's not really a part of this project right and if this if moment is something this project uses I want it local I want it with me and in fact when we get into packages you'll see that I want to save the fact that this is a dependency of this project Global is something that you use when say like Microsoft Azure allows me to use node 2 to to maintain my services I can go create websites I can spin up VMs shut them down all these things in and I could do all of that using a node module called as your CLI it's like the command-line tool that works on Windows Linux Mac everything for commanding your your cloud subscription well that is not something that I want to put into any given node project it's something that I just always want to be able to do on my command line like Here I am in this project sure but if I type a sure it's gonna say yeah I recognize Azure as yours this tool that you have so node kind of spans these two worlds it's a really good command-line utility engine it's also a really good engine for running the JavaScript that's inside of your app we had a couple questions yeah yep absolutely well so the question is moment is a package that I pulled in where do I go discover those packages and we'll get to that in a little bit when we get into the packages yeah where does it install it that's what we're gonna look at right now awesome question so I just did npm install moment if i would have said in npm install moment - g4 global it would have put it in that node path folder okay but here I said just simply npm install moment well now if i do an LS i see that i have a new folder node modules if I go into node modules I have a moment folder so I have one module right wrong I have many modules underneath moment moments gonna have dependencies if I go into moment and list lo and behold they just made a liar out of myself moment apparently doesn't have any dependencies which is kind of cool but let me go back up and say npm install Express this is a web a very popular web framework endnote it allows you to create websites real easily and Express I happen to know has a whole bunch of dependencies there you can see what exactly was installed so now let me go into my node modules folder now I go into Express and it itself has a node modules folder and it has all of these as dependencies sometimes when you move into the world of node you were surprised at just how many dependencies certain things have because of the nature of it each of these things does one small thing yeah they're very well maybe duplicates when it climbs the tree it'll pick it'll basically the the reverse recursive nature of it will mean that it's going to pick the the one that the package creator wants the most if he brings one local it'll use his otherwise it'll use another one that you have defined in your project higher up so yes sir right yeah the versions that's right so the question is how do we handle versions what if I have a dependency on moment but not moment 2.3 I need moment 1.8 well when we get into the packages section here in a little bit you'll see that we a package is basically defined by a package meta data file Pat it's called package jason and we'll see that soon and in there you describe your dependencies and when you describe a dependency you have good control over what version it needs to be you get to say it has to be exactly 1.1 or it can be anything after 1.1 go ahead and get the latest one or you can say it has to be one dot major version of one but eight or nine whatever just get the latest minor version so you get really good control over exactly what version you're dependent on and then if that's not fulfilled anywhere else and then it's your gonna you're gonna have that version in your node modules folder so you've got fine control over exactly which version you're going to use well think so the question is what if I have two modules than they are dependent on different versions of it well those dependencies are described recursively inside of those modules subfolders so we don't have to worry about that by default in fact the thing that we have to worry about with node is what if I want to share the same exact node modules folders and with the latest version of node that's coming out of MP M that's coming out soon it actually uses a really interesting trick to flatten the directory structure and be smart about keeping multiple versions but still should but but introducing the ability for it to share them but the the problem that you raised with what about my two modules that are dependent on different versions it's not a problem at all because they maintain their own completely separate instances of those sub modules and their versions ok let's move so I created let's flip back to visual studio code here and I created this moment and it should now work because I've installed it so now let's node this directory and it says no problem and in fact if I console.log the moment it works and it's spitting out this moment object okay so what about creating and consuming a custom local module let's give that a shot I'll switch back to code here and I'll actually use the I'll use is as opposed to just using touch and doing it at the command line sometimes I bounce back and forth back and forth between my IDE in here well now let's say I want to bring in something called a circle calculator I'm just gonna create a circle calculator and I'm gonna require something local okay it's gonna be called circle let's just let's just call this calc okay so I've got a thing called calc that I want to create and bring in now can you guys see what I've done wrong already that's right if I don't specify a path indicator at the beginning then it's gonna look and say it's not a core module it's not a third-party module it's not your global I can't find this thing called Cal we have to tell it oh this is mine and I can do it with a relative path like that I can climb the directories what you know whatever I can I can find this as however I want I can even use an absolute path although that's not recommended it's good to make your whole application relative so that it's you know more portable okay so I'm gonna bring that in and then I'm going to at first I'm just gonna spit this thing out just like we were with the the other ones now we really don't need these I'll just go ahead and comment those out so they're not in our way I want to bring in this thing called calc well now to create that I'm just gonna come over here and create calc is by the way this Visual Studio code is cross-platform it works on Windows Mac Linux you can actually spin up Linux X install Visual Studio code and use it there no problem and it's it's really slick like I said it opens usually sub-second I've never seen it taking more than two seconds to open so that's what absolutely thrills me and it's got some really good features like debugging get integration and and task runner integration which is really nice I can do things like I can come up here and say what can I do with tasks so I can just run a task and so I say something like tasks my task and whether it's a gulp task or a grunt task or whatever it's gonna run it really really cool environment ok yes go ahead yeah the question is why is this called index.js it's a convention it's a convention to call your your entry point to your node project index j s and it's such a convention that node recognizes it built into the engine it recognized it looks for a file called index j s and says if you don't tell me where your entry point is I'll choose this just like if you're writing a C++ application you have a main function and you don't have to go configure that main is your entry point it looks for something called main and says I'll start there it's got to start somewhere right when you boot your computer it's got to got to start somewhere okay so in here I need to create a custom module custom modules are very simple there's one concept that you need to know and that is that you have you have something injected into here called module and you can access it so I can say module exports module has a property on it called exports and whatever I set module exports to is what this export returns okay it's a pretty simple concept so if I say module exports equals 3 that's gonna work just fine this is a complete although very silly module ok now what I what you'll normally do is you'll set it equal to some value like this and you'll return some things so let's return an object this is a calculator let's say we want to have a property so we're gonna have the pie property and we're gonna do PI three point one four three point one four one five nine two six five three five eight nine seven nine three two six four three three two three eight seven four eight three two seventy-nine that's that's close enough for me and now I'm gonna have a function area an area is a function of a radius that's going to come in and in this function how do I calculate the area of a circle based on the radius PI R squared would be the circumference two PI R so two times R times this dot PI I have to refer to it as this that's the the thing inside of this same object so there we go I've written myself a pretty nice little calculator that if I tell it a radius it's going to it's going to calculate the area for me so I'll save that jump back over here and now instead of console logging out the calc that just that actually let's go ahead and console.log that out and then let's console log the result of calc dot area of seven okay give that a try see if it works and it says I can't find the module slash slash calc you guys let me leave two dots in there it's right here in this same folder it's not up a folder it's in it you saw it didn't you you thought what's that semicolon you know what I think it would be optional right there the JavaScript engine still has an optional semicolon sometimes it bites you I'm always explicit about it all right let's node this so there's our object and why did we get undefined that wasn't supposed to happen oh you know what you guys we're not we're not paired programming if you don't give me feedback here we need to return this value from this function otherwise a function that doesn't return anything first of all it's just fine in JavaScript it's very very forgiving which which burns us it's very forgiving and if you write a function that doesn't return anything then the return value is just simply undefined Ariat oh you're right yeah you're right dang it shoot hey no comments from the back so you're saying that I didn't memorize PI to the 30th digit correctly let's try this again one four one five nine two six five three five eight nine seven nine three two three two six four three three two three eight four six eight three two seven nine is that correct can you look that up for me okay thank you okay and this is you know what I'm gonna do is the easy way out here I'm gonna call this the circumference and let's just yeah let's I'm just gonna change the requirements all right I know better than my than my customer what he wants so look at this intellisense on that function is that glory or what oh there's so much glory actually in in Visual Studio code they're in no this is more of an ode concept there's a project called TSD that I'm jumping ahead I'm this is just a freebie I'm gonna say NPM lists global with a depth of zero and show you what my globally installed packages are and this one on the bottom TSD allows me to pull in typescript definition files and Visual Studio code can use typescript definition files to give you really good intellisense like strong typing on any node project that has a TD PSD file which is a lot of them so now in here remember I installed Express I did an NPM install Express I could say for instance TSD install Express and it's gonna go look in the TST it's actually looking at a github repo and saying oh I there is a tight script definition file for Express I'm gonna install that for you into this folder called typings which will appear in just a second there it is typings expresses in there and now when I use Express have you guys used like c-sharp with all the strong typing it's like heaven right and then you move to JavaScript you're like wait what you now you get it now you get all of that really really rich typing so if that was just a freebie that was on the side there okay let's move on by the way I just returned what's called to the namespace pattern I returned an object that had a property in a function right that is the simplest pattern in JavaScript but there are all these patterns for what your modules can return and a lot of people instead of just returning a namespace they'll return a function and the function may be a high order function or a constructor for constructor function or a singleton function these four kind of all very similar but a lot of people return a function so when what you get back from your require you instantiate with it or you otherwise call that directly okay so you'll see these different patterns for what can be returned in my opinion this kind of makes the module pattern more rich or a superset of the class analogy that I brought up earlier so that's kind of cool this I pulled from this post by the way all of my slides today actually there's this and my tweet monkey deck I put in the same onedrive folder and you can access them at this short URL and this will stay on the screen so you don't have to write it down right now anyway so you can go here and you can click on that and get all this information about those different patterns okay so now let's talk about packages after question yeah yep yep so the question is I see that Asher and Google App Engine will both run Web Apps what is that and if I have JavaScript dimensions why would I why would I need that in short web apps is just webs web projects running in the cloud and in handling a lot of the hard stuff for you like the setup of your of your whatever your server engine is whether it's no js' or iis or or nginx or whatever and you need it because this and nodejs is hosted in a web app it has to be hosted in something and so on as ER you can go create a web app and publish a no js' file like you don't have to publish anything else about it except like you could just put an index dot JSP up and it'll say this looks like a no js' project to me and it will run that in the web app using node as the engine so it's like the cloud container that contains the node runtime that runs your JavaScript is that clear cool okay so packages need to be defined and differentiated from modules when I first got into this I was I was talking to somebody I was like oh yeah well modules packages same thing and he was like no they're not the same thing packages are containers for modules they contain one too many modules and it's like well that's interesting usually I just have one module and he said that's right and most of them you only have one module but it's important to differentiate the two and that's exactly what he said is exactly right a package becomes like an envelope with a metadata file that says hey I am a node.js package I contain this module I also might contain these three others and here's some information about me the important things is that it's versioned so all the versioning needs to be well described so that when somebody creates a dependency on your package on your module via your package they can very explicitly describe exactly what version they they want or need it it defines all of the dependencies of yours so when somebody grabs your package as a dependency you get a chance to say and I depend on this this this and this and so when they install it it's gonna also install those recursively it's usually published online that's the delivery mechanism for these packages I push it up into the store online other people pull it from there I could just hand it to you on a floppy drive but you know it's 2015 by the way so a package is defined by the presence of a package JSON file that makes it really really easy to turn your node app into a package a node package let's go back to my command line and we have created this super cool super useful thing called the circle calculator with a little bit of help from the peanut gallery in the back thank you and I've got my Taipings I've got my node modules I've got my calc J s I've got my index J s great I'm ready to deliver - you were you were you right wrong I don't have a package to describe the version of this - to describe the modules in it and to do some other stuff now to create a package it's a really easy I create a package JSON file I look up the JSON schema and make sure I get all the properties right I fill out the property values blah blah blah or I just use NPM in it now NPM in it remember I told you NPM handles the whole packaging story that's what it's for this whole job in life doesn't do anything else an NPM an it walks me through a command line wizard for creating a package for this current directory where I'm in so it says oh I noticed you're in a directory called first my first node app that sounds good for a name right yeah sure enter how about version 1 is that a good place to start yeah sure how about a description blah how about the entry point okay now I don't I don't have to have I don't have to define an entry point but it's nice to do it explicitly index J s is my entry point I also have calc is that where I start no that's one of my local modules I want to start at index J s what's the test command skip it gate repository I don't have it in github yet skip it keywords do I want people to find this I need to enter some some good keywords that's a good idea what's the license for this package that's another role of a package is of the package metadata file is to describe what the license is for this I use Apache two by default so I set that up as my default so whenever I run this that's what it's gonna use and then it says ok I created this package JSON file for you does this look good sure and now I have a package.json file really easy and by the way if I delete this package JSON file I can also say NPM in it - why meaning use all my defaults and just make it for me don't walk me through that silly wizard and now I have a package JSON file that looks like that super super easy and I just turned my module into a package ok any questions on that you guys understand that okay now I have some dependencies in here right I have this dependency on calc which is one that I created and if I jump back to here I'm gonna say you know what let's take this dependency on moment I'm not actually using it right now but that's fine I'm gonna pretend like that's one of my dependencies well now let's delete let's delete this entire node modules folder and now let's look once again at my package.json file and when I created that with NPM init notice it said I noticed that you have a node modules folder that has Express and moment in it so I'm gonna go ahead and define those as dependencies but that was a condemned as a real convenience that it created those for me but let's actually go to Visual Studio go in here and delete those dependencies let's pretend like it wasn't able to do that for me or I hadn't I hadn't installed those yet so what I want to do is I want to say in this application like if I run this it's gonna say yeah you don't have a moment okay I need to install moment right we did this already but moment is something that I am taking on as a dependency of this project I need it I'm gonna need it into the future so I say save and what that does is let's go over here and look at my notice I deleted the dependencies property in my package file here I'm gonna hit save and it's gonna say ok I'm installing moment for you and I put it into your into your package Jason as a dependency I can also remove shortcut is are by the way I can also remove and save and it pulls it out let's go ahead and put that back in ok how many times do I have to do that to make sure that we know it we're done yes it it just feels silently doesn't do anything yeah I wish that it would earn it with yes and save it as a dependency that would be cool but I'll talk to my friend that npm I was telling everybody I think before a lot of you got here that I have a podcast called code chat and I interviewed a guy that works at NPM and it's been fun knowing him because I can say hey why does this work this way he's like I'll go ask Jim see why it works that way yes we're here okay so I'll answer this quickly because it's a bit of a rabbit trail but in this package Jason if you go to NPM documentation there's a lot of functionality that you can drop in here it's really nice you can define arbitrary scripts and then using NPM you can run those scripts so it's good for a DevOps story that's they've got tests there but you can have whatever other scripts you want you can you can I almost like replace gulp you know it's it can almost treat it I would still use gold by the way but you can almost treat that as your task runner as all the things that need to happen to condition or test or build or deploy or otherwise run your your application okay so where are we we added dependencies we removed and installed those dependencies package repositories I told you about NPM j/s and somebody asked the question I told you we'd get to it soon where do I go and discover those packages I go here all the time NPM I don't go to nmp JSONP MJS org NPM j/s is org is the website for the package store okay now the package store you normally interact with it from the command line I want to install I want to search or whatever you can you can do it from there but you can also go to the web here and you can say well what about this moment one that we've been working with search for it and it says well there's a moment right here and look at this two hundred and forty three stars that's really good and if I really drill into it and look at it I can see what version it's who the contributors are how many times it's been downloaded look at this in the last month two and a half million times this one is very very active here are the dependents the the there's almost 4,000 people that are dependent on this which doesn't sound like a lot if it's been downloaded two and a half million times in the last month but so this is telling you what you use this for is is this the thing that I want to create a dependency on do they have their stuff together or is this some fly-by-night that made something called moment and it has a few functions in it nobody really uses it from this page you get to really see if this is something that you want to use okay so I go here all the time and the other thing that's nice about it is it tells you and by the way here's where it is on github so this is an open source project I can jump right over there the source and I can go create an issue for it so I'll actually when I'm dealing with somebody tells me there's this really cool node module called moment go check it out I'll go here look at the Stars and everything I can star this myself yeah I like this one and oh I'm not logged in so it's gonna ask me to log in close your eyes okay so I have whoa have you seen this wombat I might actually it might actually have worked and logged me in I can't apologize for their errors yes no sale okay it's matter anyway so I can star this myself and say yeah I really like this this package this module so if you guys see how it gets a sometimes it gets confusing what's the module what's the package moment is both a package and a module let's jump out to its github my mouse is where luckily I have a touchscreen there is okay so if I go out here and I look now these guys followed a different convention remember I told you index J s is the default entry point well these guys said well let's make our entry point moment J ass I don't know that's a I've seen that people use the package name as the name of the JavaScript file that is the entry point but that's gonna have to be defined now because NPM doesn't say moment is the default entry point so if we go look at the package Jason for this notice the main that's saying moment is the entry point for this that's that's how we that's where we begin with running us but notice all of the all the stuff that's in their package JSON file you can tell that there's a lot of stuff I'm leaving out of of a presentation with this scope you can put all the contributors the author you know where the home page for it is and everything okay now on repositories are places online where you can go get your node modules you can install them and this so far whenever I hit install those come from NPM but NPM is a group of good people and they said let's not just make everybody do things our way a lot of people store their stuff in github right so let's let them use github as a definitive source for the location of their packages as well as well as NPM now NPM is far better than github at delivering final packages so I would choose that as your means to deliver final package that's exactly what it's made for but if you've got something in github maybe you've got a private github repository that your team shares and you want to put a module there in github and you want to use that as your package source you can do that as a matter of fact all you have to do is at the command line remember when I said NPM install moment did you anybody notice what moments github repository what's called it was yeah it was moment slash moment right in moment is there their username on github and moment is the name of this one if I say something slash something NPM says oh you're getting this from github aren't you by default if you say something slash something it says okay I'm gonna go on github to HTTP colon slash slash github calm slash moment slash moment get that's the URL for that repo on github and I'm gonna pull it down from there now there's more freedom there on github you could have whatever you want in your github repo we're gonna expect that if you're trying to install a node package from here that you're gonna have a package JSON file you're gonna have an entry point you know it's it's got to look like a node package whereas you're forced into that when you put it in NPM and github you're not so you better conform otherwise this is gonna blow up with red and say I've try went and looked at github where you told me to and it didn't look like a node package so I can't install that we're not gonna do that right now we're gonna move on okay so we looked at requiring a module by specifying the name of its package and what it does is it looks at that entry point and pulls that in but I told you that a package can contain more than one module often I shouldn't say oftentimes does sometimes does if you want to use the other modules that are inside a package you have to know something about it you can go look them up in the source code or whatever but basically you just use the name of the package and then you append the path to the different modules that are in it so it's kind of a convention to have a Lib folder with a number of JavaScript files in it that are the different modules that this package contains and so I could require my module but then I could also require my module /lib / another module that's in the same exact package okay that's all I'm saying on that it's not very common so yeah yeah the question is can I have a local repository there's a couple ways to do that first of all NPM has the just like github has the concept of a public repository and a private repository and you have to pay for the private repository NPM same thing if it's public it's free you can have private repositories and then you could share that repop you can just keep that repository for yourself I think it's something like seven dollars a month or you can use that for your team and they also recently in Tyrell fits completely introduced but he told me I'm allowed to tell you that they recently introduced something called organizations so you can create organizations and use that to kind of organize your and your company or whatever you also can install a package from local now it's not necessarily it's a little bit more brittle in my opinion but you can you can just where I said you can npm install something from the npm repository or something / something to get it from github you can also npm install a local path and it'll expect it to look like a node module and it'll bring it in I'd never do that and I I don't like it because like I said it's a little bit brittle what I would recommend is that you get a private repository or do something called NPM linking which I'm going to show you in just a second that's right yeah I think a better thing to do in that exact case is NPM linking and we'll see that real soon so okay I think we're running a little bit short there's a number of package conventions I'll skip this kind of you saw the Lib this is the only one that like must be there package Jason that defines a package everything else in your node package is by convention it can look like kind of whatever I was gonna take you into Express in a moment and show you how they do or do not follow the conventions but I'm gonna kind of skip that installing packages oh I'd these builds lives I forgot to fix so they're all goofy so you can install we already looked at that save - all save does is put it in your package JSON file if you want to install something globally you add the dash G so gig is a really nice node module that Jenner get ignore files for you have you ever said like I want to start a Python project what's that a good default get ignore file for Python well you can go online and find it and copy it and paste it in a text editor and save it and check it in blah blah or you can just say gig generate icon and it goes there you go I should get ignore file it's really really nice I use I use gig generate node all the time creates meme I get ignore file for node and it ignores things like the node modules folder which you don't want to check into your source control very very nice so that's one I want install globally I want to use that anywhere wherever in that and NPM install production is really nice that ignores the dev dependencies so you have this ability remember when I said - - save - - save saves it into your package JSON as in the dependencies section but - - save - dev saves it into the dev dependencies section so something like gulp or grunt or J's hint or lint or whatever whatever you use as a developer for your stuff that doesn't need to be developed or that doesn't need to be installed when it gets to the production server so you save it as a dev dependency and then on the production server you run this line and that says I'll just install the real dependencies not the dev dependencies this is depth zero that's supposed that's a typo that's supposed to have two dashes depth zero you saw me do when I listed off mine if I say NPM list it shows me all the dependencies in my current directory but it shows them in a full tree which is sometimes very nice and sometimes it's just too much info so here I am in my first app so I'm gonna say node or NPM list and it's gonna say okay here we go we have a dependency on moment now if I had expressed then I would see a full tree with all of them in fact I'll say NPM install Express and we'll just pretend that that's a real dependency so we're gonna save it this should be very fast because I installed it just a little bit ago and it caches them which is a whole nother topic that we're not talking about so like I said this should happen really fast so anyway when I list this it's going to list Express and then a little subtree it's gonna show all of its dependencies and their dependencies in their dependencies so there's this really cool tool called that you can do scaffolding and so you can say something like yo web app and it'll create you a web app very very nice it's hilarious though because when you say yo web app the dependencies that you get in just the default web app or the default angular web app or were you know these default templates in yeoman have something like 30,000 node modules if you look at the full tree it's amazing it's absolutely amazing I mean I'm used to you know tens or maybe hundreds of packages that make up my project but tens of thousands that's amazing okay let's kind of move on here I use by the way I use this list G all the time to see like have I installed that yet is that in my Global's whatever you can remove packages by simply saying remove you can also remove them and take them out of the package JSON file with that save and by the way a lot of these have shortcuts so R is like you saw was a shortcut for a new loop seeing just NPM R and that and then the name of your package oh you know what I I really should have put the name of the package right there NPM removed packages otherwise otherwise you know what I so this is an issue that I pushed back to the NPM team and they came back to me on this has an effect does anybody can anybody guess what that effect is I just say NPM renew they would love this feedback like what do you think remove should do if you call it on its own I'll tell you what it does it says okay you have a package here that you're in if you say npm remove it it removes this package if it's globally linked which we haven't talked about yet so maybe this is hard to understand it removes the global link from your global area so it actually has an effect it's and it happened to me and I was like wait what okay so this is linking and this is really nice if I'm working on this this my first node app right and I want to use it and this is gonna address his question whoever his question about if I want it if I have something locally on mine and I want to use it how do I do that well here I am in my first oh come on you're killing me here I am in mine if I say NPM link okay first of all what I'm gonna do is I'm gonna go to my node path so I'm gonna I users thank you is it roaming now yes roaming NPM this is the location of my global right you guys remember when I listed my Global's and that's what they were yeah I think we have six minutes left all right so here they are and now I'm gonna go here and I'm gonna say NPM link I'm in a node app and I'm gonna say NPM link this means like me NPM link me when I do that a symlink is created in here which means that from elsewhere if I want to use this package that I just created I can do that by simply installing or by simply referencing it or what I can do is I can go to what's what's better to do is to say NPM link and then I refer to my first node app from here and I'm linking to that one via the global directory so now if I go into I'll just do it this way if I go into my node modules I have this my first node app in here now I'm using it but it's all just symlinks I'm actually using it from that other place and so that's really handy if you forget it just remember sent an NPM link and go look it up in the documentation but that's what it's for and it's really really handy and then you can NPM unlink to pull those out okay publishing packages this is short and easy like I said this is one of my favorite things about node and NPM is that getting packages into the store is extremely easy first of all NPM login it's the same as NPM add user from the command line if you say NPM login it's like username Fred password password enter you just created a user in NPM in fact if you go to NPM jst org their website you can log in using Fred now okay so it's the same user on the command line as it is in their website and you log in to log in and what that actually does is it creates a dot NPM RC file in your user profile and it stores an authentication token so that from now on on the command line you don't have to log in anymore it's gonna remember it but it's also not storing like a base64 of your password so you don't have to worry about that I'm gonna I'm gonna say NPM logout is pretty obvious now once you have done this and created yourself a user it opens up the ability to go to the web login do all this stuff on NPM j/s that you might want to do like see your stars or add a star you can also NPM star on the command line so I can NPM star Express and say I like Express which I use that a lot actually because it's really nice too when I reload on a new system or something and I want to go reinstall my global node modules I'm like what do I like which what are the ones that I I want to install him so I'll go look at my list of stars and install those ones so once you're installed there once you're logged in rather to publish a package you do this npm publish that's it that's it all you need here are the minimum requirements for publishing a package needs to have a package.json file and that package JSON file needs to have a name and a version done that's it so I I was blown away by this because I've kind of already written my project I've described it with this one file and now it's already ready to go let's go back out here I I'm not going to do this because it's just kind of rude to publish packages that don't add value to anybody's lives but if I go to this package if I say npm publish right now it would work it would actually push it in to npm i would be able to go out to npm j/s and search for it and I would find it so really is that true I'm not connected is that true my jetpack yeah okay I would be able to go here and for instance I've published one called Candyman and that's each by code foster I can go to it you can all go to it right now and hit start for me there's the version there's where it exists on code hub on github this is my little description of it and there are a few things that are oh and by the way you can unpublish it okay finish my thought on the next slide when you unpack you can never fully unpublish something but if you say unpublished then it's effectively not even nobody's gonna be able to use it they're not gonna see it but it the reason why I say you can never fully unpublished something is if I publish something that is Candyman at version 1 and then I unpublished Candyman at version 1 and then I come back and I say oh I want to put I want to publish Candyman at version 1 it's gonna say no that in the some at some point in the past that's been done already you can never ever ever do that again so be careful with your publish because you can never fully undo it that version number is reserved forever an NPM you can also deprecated packages which is really handy you can give people a message that says we had to defecate this one because we found out that when it you know whatever and then people know not to use this one but it's still there you don't totally break their there's that now there's some best practices when you're publishing do have a readme DMD because that's what's used on NPM j/s org to describe your thing so you saw my Candy Man 1 I have a little sample code how to use it stuff like that in there increment your semver there there is a utility in NPM you can just say NPM version major and it will increment the major version of your package NPM version minor NPM version release and all of node packages all of NPM use assembler you have to conform December for your semantic versioning for your versions don't publish placeholders I have two placeholders published before I knew this the the etiquette but don't publish something because you want to reserve the name and you haven't made anything yet you know like have something before you put it out there and also make sure that you have a link to github or to your online or your source control wherever it is and be sure to check your issues you you you want to be you want to maintain your package well so that people will actually use it this we're done this is my team that's here at code camp if you find any of these people they'll kind of be able to point you to the others that are here so if you have a question about gaming I won't be able to answer it but I'll be able to point you to one of my colleagues who can so it's a pretty pretty cool team and I really like my colleagues we have a number of MVPs here as well raise your hand if you're in the audience and you're on screen anyone anyone all right or MVPs know everything about nodes so they didn't come to this um so yeah you can go over to the booth and get a free book if you fill out a survey and you can play with cool devices and there is a raffle today I'm not sure exactly when but if you fill out the survey which is that akms slash SBCC at lunchtime thank you if you go here right now and fill that survey out you'll be entered to win a Dell Inspiron two and one and or or or a Microsoft band so good all right thank you guys enjoy the rest of code camp appreciate it being here hey you you
Info
Channel: Silicon Valley Code Camp
Views: 12,561
Rating: undefined out of 5
Keywords:
Id: OTE8_lh_6MY
Channel Id: undefined
Length: 75min 43sec (4543 seconds)
Published: Tue Oct 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.