Node.js Crash Course Tutorial #5 – NPM
- December 29, 2023
- Posted by: MainInstructor
- Category: C Go JavaScript Node React
Video Title: Node.js Crash Course Tutorial #5 – NPM
I rather than gank so far we’ve seen how to use core modules and packages that are built into the node core for example the FS module to work with the file system but what if we want to use some additional packages for extra features in node that are not available in the
Node core now this could be for something like a JavaScript utility library like lodash or maybe a package to help with validation logic or it could even be some kind of framework for building node websites like Express there are literally thousands of user made packages that we can use with node
And we can install them using NPM the node package manager now NPM is automatically installed onto your computer when you install node and it’s a tool that we can use to install update or remove node packages onto our computer directly and also into our individual projects to use now you might
Have even used NPM in the past for installing front and frameworks or libraries like view and react or something else but we’re going to be using it to install packages that are going to help us with our node websites so you can reach the NPM site by going
To NPM j us com that link will be down below and on this website we can search for different packages we might want to use in our project so for example I could search for low – and if we click on that result we can see how to install
Low – and how to use it in node and also hopefully a home page and a repository to check out the docs and how to use this package another package is no daman and this is the one we’re going to be installing first of all onto our
Computer and this helps us with a development workflow so we don’t have to keep restarting the server manually so I’m going to use NPM first of all to show you how to install this globally onto your computer okay then so no daman is a package which
Helps us to create a live reload server and that’s really gonna speed up the development process because currently every time we make any kind of change to our server code we have to cancel out of the current process in the terminal and then rerun the file again and this
Restarts the server to reflect those changes in the browser so let me just demo this if I change this redirect here from about me to about us now I’ve changed the code I would have to first set the file then cancel out of this current process by clicking ctrl C then
Run the file again by saying node server and then pressing enter then I could see those changes in the browser when I make a request so I could go to about us now and it would work it would redirect form it but this would get tiresome after
Every code change that we make having to restart the server down here manually so node Mon helps us to combat that by automatically restarting the server whenever changes are made and saved to our file so first of all let’s install node bonnet down here first of all
Cancel out of the process then to install a package using NPM we say NPM install now if we take a look at the node mom package over here it’s going to show us how to install this package so just scroll down and we can see right
Here it says NPM install – G which means install globally on our computer node one right here so because we’re installing something globally on our computer it means we’re not just installing it for our current project right here it can be used anywhere on our computer so essentially we’re using
NPM to install a program globally on our computer right so we can use in different projects in the future as well so let’s do that we’ll pass in the global flag to say we’re installing globally and then the package name which is node 1 press Enter it’s just going to
Take a few seconds to install that package alright then so once that’s done instead of writing node server to run this file as we normally would now we can use node montt because we installed that package and then the file name server and this does the same thing it’s gonna
Listen for requests now on port 3000’s but now it’s set up a live reload server and it’s watching our code so if I make a change to about blah and save that then watch down here it automatically restarts the server due to changes we don’t have to cancel out of any kind of
Process and rerun the file it does that for us and if we go over here now we can just go to bounce – blah and this is gonna work so that is a lot easier than before where we had to manually restart the server every time we change our code
So that is a package that was installed globally on our computer but what about packages that are specific to our project alright then so the package file is a JSON file and it keeps track of any packages we install locally to our project and other things as well such as
Project details and any kind of project specific scripts now if you’re thinking of using any kind of third-party packages at all in your project then you should definitely create a package JSON file so we do that by coming down to the terminal and saying NPM in it and this
Initializes as a package JSON file now it’s going to ask us a series of questions about our project and it gives us a default value in brackets if we want to accept them we can just press Enter otherwise you can override those by typing in a new one for the most part
I’m going to enter through these and just accept the default values because they’re all fine but you could fill these in if you want to okay so once you’ve gone through that it creates us this package JSON file and by the way you should already see a package lock
JSON file as well and that file right there keeps track of the different dependency versions that we have installed in our project we don’t need to go in and edit anything at all inside that file this file right here keeps track of a few different things about
Our project such as the name the version all of the different questions that we answered over here we also have some scripts right here that we could run for this project and we might look at scripts a little bit later on but more importantly this package file will keep
Track of our project dependencies and by dependencies I mean all of the packages we install locally into this project those packages will become things the project depends on hence them being called dependencies so any node project that you create and you will install third-party packages for that project
You should definitely create a package JSON file for that project by running NPM in it okay then so now we have that package JSON file right there we can go ahead and start to install packages locally to the project now these packages could be like utility libraries date/time
Libraries frameworks etc which help us implement extra features into our code and project so what I’m going to do is install a utility library called lodash so if we go to the NPM website and search for this lodash right here and click on that we should see the
Installation right here so NPM I which is short for install so you can use this instead of the install keyword if you want to save with double dash in front of it and then a low – now this say flag right here this saves it to our local
Dependencies for this project and that it’s going to register that inside the package.json file so it can keep track of that now if you’ve got a new version of node and NPM you don’t need to explicitly use this say flag it’s going to install it and save it automatically
To our dependencies but if you have an older version then you should use this if you’re unsure just use it to avoid any kind of doubts so this is what we’re going to install but before we do that let’s open up the lodash homepage as well and take a look at the different
Things we can do if we go to the documentation so you can see we can access a lot of different methods using underscore and then dots so underscore is the lodash library that’s what we are required from it when we install it and then when we use that we can use a lot
Of different methods utility methods from that library so let me go back we’ll install this first of all then just try out a couple of the different methods so I am now going to come down here and say NPM install or you can use
I and I’m gonna install a low – now I don’t need the save flag because I’ve got a recent version of node so if I press ENTER it’s gonna install that locally for me and we can see now inside the package.json file this dependencies object right here and inside that we
Have low – and the version number so this is keeping track of what package we are using or what packages we are using in this object and it’s saying that lodash is one of them okay so let’s go ahead and try to use lodash in our code so in
Server j s the first thing we need to do is actually or require that at the top so I’m going to say Const and then underscore is equal to or require and we want to require low – now node is automatically gonna know to look inside the node modules folder right there
Which is just magically appeared and that appeared when we installed that local package so whenever we install a local package into our project it’s going to create a node modules folder and all of the different files and folders that are needed for that package and the dependencies of that package are
Going to be kept inside node modules but we don’t need to directly go into this folder ever okay they just kept there so what I’m going to do now is try to use this right here lo – and by the way you can call this what you want you can call
It lo if you prefer but common practice is just to use the underscore and then down here inside this function right here I’m just going to try using it so let me come right to the top and in fact we’ll get rid of this console.log we don’t need that anymore
And let’s do instead a little comment and say a lo – okay so say I want to get a random number lo – provides us with a utility method to do that so I can say Const num is equal to a low – dots and
Then we use the random method and it can take in two arguments and those two arguments are what we want the number to be between so the boundaries so I could say zero and 20 for example and that’s going to get us a random number every time this function runs that is between
Zero and 20 so let’s try and login that to the console console dot log and we’ll logout none okay so we need to run this and we’re going to use node mana to do that so no daman server and press enter and it crashed it says it’s waiting for
File changes before starting so something’s not quite right unless have a look what that error is okay he says cannot find module load – and that’s because I’ve misspelled it right here it should be a lo – not load – so let me save that again come down here notice it automatically
Restarts the server when we fix that error so let’s go to the browser now and let’s go to for example just forward slash and we get the homepage back everything still works but now in the console we can see this random number 17 if i refresh we’re going to get a
Different random number 18 if a refresh again it’s gonna be something different 12 so this is pretty nice this is just one of the different methods we can use all right so let’s have a look at another one so let me create a function I’m going to say Const greet is equal to
Some kind of function right here and our function and inside here I’m just gonna say console dot log and then we’ll say hello right now imagine I only wanted this function to ever be allowed to run once once only well I could use lodash
To do that so in front of this I’m going to say low – dots once which is a method in itself and inside here as an argument we pass the function that we want to execute only once so now if I call the function down here it’s going to work
But if I call it again it’s not gonna allow me to run it twice and we should only see this once logged to the console so if we save that and I hurry flesh over here we can come down here and we see hello only logged at once to the
Console so that’s nice as well and like I said there are loads more different methods we can use with low – but this is not a low – course this is a node course and I’m just showing you how to install third-party packages here if you want to learn more definitely check out
The lodash website and you’re going to see all of the different methods and properties we can use in the documentation right here okay so there are many other packages that we can use as well as this one and we’re going to see much more of them as we go on
So another good thing about NPM and the package.json file is that it allows us to easily share project code so imagine I wanted to share all of this project code with friends or colleagues or something then I could email it to them or I could upload it together for them
But this node modules folder right here could be absolutely huge with loads of different packages and dependencies inside it now I really don’t want to upload that for them for that reason I just want to send them or upload the project code that I writes so if I for
Example upload all of the project code to github like I’ve done here for my course files then you’re not gonna see that node modules folder and typically people won’t do this they won’t upload the node modules folder so if I was to download this code by going over here
And then clicking on download zip if I unzip that folder then open it up in a text editor I’m not going to see this node modules folder and it’s gonna look something like this if I right-click and delete it it’s gonna look something like this
Now if I try to run this let me cancel out of this process and run it again node one server then I’m gonna get an error and that’s because it cannot find this module lodash and we’re trying to use that right here inside the server
File and if we were trying to use other packages as well because we don’t have the node modules folder where all of those packages live it’s going to error because of those as well so we can’t run this at the minute first of all we have to install all of the different packages
That this project uses now remember they are listed inside this package JSON file right here under dependencies so we know of the different packages that we need to install and there could be four or five or even more here so we’d have to install all of them now fortunately we
Don’t have to say npm install lo – then npm install something else and something else all separately we don’t have to do that when we have a package JSON file which lists the dependencies all we have to do is say npm install and hit enter and that’s going to look inside the
Package JSON file this dependencies property right here and install all of the different dependencies listed right here and that’s going to do all of them in one fell swoop so now if we try to run this by saying no daman server and press ENTER then it’s gonna work without error so that’s another
Nice thing about the package.json file so if you’re ever looking at a project that uses node on github chances are you’re not going to see the node modules folder and the same is true for my github course files so if you’re trying to view the code for a specific lesson
For example lesson a or seven you’re not going to see that node modules folder and if you download this then when you open it up in your text editor first of all you need to run NPM install to install any dependencies first and then it will work so then now we know all
About NPM and the package.json file and how to install different packages in the next video we’re going to install another package called Express and this is a framework for node that helps us to easily create node websites so we’re going to learn all about that in the very next lesson
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
you are epic
If the package-lock.json file does not appear for ya, then write: npm config set package-lock true
Then run: npm install , and it should appear.
Thanks! Very well explained! 💛
hey dude , is the "lodash.once();" submodule doesnt work anymore ? , I tried it manytimes … the sever is still listening for the greet request more than only once !! (the syntaxe is correct)
paackage-lock.json not created automatically on my pc
why?
FOR NODEMON EXECUTION POLICY ERROR
OPEN WINDOW POWERSHELL RUN AS ADMINISTRTOR AND COPY PASTE BELOW COMMAND:-
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Please what is the name of theme that you use in vs code ?
I can only pray God add more days in this life, thanks a lot Mr. ninja
Some of you guys may have a problem running nodemon server and an error occurs tells you something about security and permissions something like that.
to solve that just go to the path given in the error and delete nodemon.ps1 and run nodemone server again and it should work.
Amigos🐎
Hello Shaun, I really appreciate the effort you put in the course, you are real thing, you really know how to pass knowledge into someone, nice work dude.❤
Your easy-explain-jutsu is legendary!
Does nodemon do the same thing as node –watch script.js ?
For anyone having issues because running scripts on your system is disabled: 1) Open Powershell as admin. 2) Type "Set-executionpolicy unrestricted" and answer yes to make the changes. 3) type "get-executionpolicy" to ensure it's been changed to unrestricted.
Thank you Mr Ninja
I rlly want you to know how effective your teaching style is. Pls don’t stop
Another amazing course from you !! Cant thank you enough!!
thanks
You are one of the best teachers on Youtube. Thanks!
Thank u a lot bro
U teach very well
that is a great tutorial
Legendary
I'm sorry to gush, but I really love the content you create 🙏 It's so much easier to follow than 99% of the other content creators out there
How funny the website would read "Ninja Parading Musically" during your video at 1:20
Sir , Am unable to run nodemon in vs code ..
Dope course man!
im the kinda guy who comments at the beginning of a video because i know it will be pure gold
When I first tried to learn about smart contracts, tutorials were saying to run npm install and I would follow it without fully understanding why. It makes so much more sense now!
Thank you so so much for this course, you're the best!!!! I hope you get paid really well because you are doing a great contribution to the community.
The accent reminds me of "Jaimie Fooking Lannister"…
the best of the best
Thank you so much Shaun.
I have been looking for a course like this for a long time. Been a fan since when I started your Flutter series. You are a great teacher, keep up the good work and cheers.
Recommended by a friend and I find it awesome just as she said.Awesome!!
You are the best ❤️ you deliver the informations in the simplest possible way
I can't install packages nodemon!
For those who are having npm package install permission issue on macbook, see the video in the link https://www.youtube.com/watch?v=nPLx3rSZPXI
if nodemon is not running due to security policy run this command in the terminal Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Never thought that such a short course on Node would teach so much…..Very neat….appreciate it
Nodemon not recognized.
Thx
When i use
const greet = _.once(() => {
console.log('hello');
});
greet();
greet();
this code , i get the hello two times. And then i remove one greet() . But the output is same!
Why is it happening????
Hi, I just want to say thanks for these amazing tutorials. You have an amazing talent for translating difficult concepts into truly digestible ones. Thank you again.
I am watching your whole videos of this node.js Playlist I am facing a problem while I am installing the nodemon in my vs code
It showing after a small process "changed 116 packages, and audited 117 packages in 3s
15 packages are looking for funding
Run ` npm funds `for details
Found 0 vulnerabilities"
If anyone knows how to solve this error please let me know…
With u node js is so easy
This is not the lodash course but a Node course
Great work Ninja