Hey guys! welcome back to the node.js series part number seven last video we
looked at what are the external modules and this video we're gonna do some
coding we're gonna create our own module on github and we're gonna consume in our
project you know we also going to consume in another external module in
our project and we can see the difference and if you're not following
the entire series please do so I'll provide a link here in the corner and
welcome to techsith tutorials! alright let's get to the coding so we're gonna
do two things first we're gonna create a package that we are going to upload on
the github this will be our external package and
we're gonna create a main project which will consume that extra package and
we're gonna also consume a package from one of the packages from NPM Repo like
lodash so in order to create our package we would have to create a folder so I'm
gonna say $mkdir a-useless-package. creative name and I'm gonna CD into it
and then if I wanna this package to be consumed by other project it has to have
one essential thing which is package.json now I'm not gonna manually create
it there is a command for it $npm init and that should create it and it will
take me through a bunch of questions so it's asking me what the package name
would be and I'm just gonna keep it use this package which is a folder name and
then word and I'm gonna keep 1.0 description entry point is I have to
have an index.js a test command I'm just gonna skip author let's just say
techsith and yes that's it so it's created it so let's look at it okay so I can see
the package.json and as I said the main is entry level entry point is index.js
so we have to create this file all right now let's create some
functionality useless.js as is one of the files and let's I have some
simple functionality so I can have some const useless equal to narrow
function which basically says console.log "you just wasted some time" alright
and we have to export this and so as we looked at in a previous tutorial if I
want to export it I would have to use module.exports equal to useless so
now our module is ready to be consumed but as I said in the package.json
we said or entry point is index.js so i have to create an index.js so I'm
gonna say indexed.js and so what this file would consume it's let's say if I
have lots of other functionality and I can have a one entry point which imports
all those modules and exports it important so I would say useless equal
to require as we know it's a local module to this so when I import it and
we're gonna export it also module. exports useless now as I said if you
have more than one then we would create a named export so we have bunch of
functionality within this and to upload on github you have to git in it so it
initializes a git then I can say $git add . and $git commit and then to say initial
commit and then I have to create this project on the github so I can push it
so what I can do is I can create a new repo and I can create a package now
already created says it's not gonna let me do it but this is how you do it and
click create repo and it would give you two commands right and then you would
type those two command to push your project onto the git alright so now
let's create our main project so for that I'm also gonna do the same thing
I'm gonna have to create a directory main proj and I'm gonna CD into it and
then I have to initialize the project so I would say $npm init and it's
asking me that's the name of it yes and it also has an entry point called index.js
and I'm just gonna go through it okay I'm gonna open the project okay so
now let's try to consume the package that we just created on my github site
so in order to consume it all right so the project is on my github so the name
of the github is called techsithgit and the project name is a-useless-package
right so if I want to install it I have to do the same thing so if I saw it say
npm install techsithgit /a-useless -package then i had to give it a branch
name and that happens after the pound sign now initially whatever you push in
it goes into the master branch so you have to use master unless you have
created your own branch then you provide that branch so it has created this entry
inside my package.json called dependency and it had put that package
and its location and if i look at it has created a a node module folder and
inside i have that package i have the index.js and its own package.js and
then the useless so i would see all the code that is imported to my project you
will also see package dot lock file so what is this package dot lock file is
pretty important because what happened is as I said before older version of NPM
had this issue where every time you do NPM install it was inconsistent so now
they created this package dot lock file which locks whatever you have at the
moment unless you change something it doesn't go out
get it okay so he blocks the version and I'm not gonna go too much into it
all right also let's install a low - okay which is an external module so all
I have to say npm install low - and i can provide a version but I don't really
care at the moment I just want to get the latest version but if I want to
install it in the in the dependency I can say - - save alright so now let's
try to use both of this extra packages that we downloaded into our project so
we need to create a file I'm gonna call it fjs okay so here we need to first
import are useless equal to require and because it's an external package it
would automatically look through the node module so it's automatically giving
it I don't have to provide a path and for lo - I would say require and the
same thing lo - now first I'm gonna use that useless and when I run that
function it should basically print that message that you just wasted some time
so let's do it I would say node app and it says you just wasted time on this
package alright in order to use lo - I'm gonna
use this let's create an array with some values so I have a bunch of faulty
values you know as a faulty value false in empty string also faulty value so I
can take the low - and I can have a function called compact which
basically removes all the fancy values from my array so low - is very useful
utility and I can say Const new array equal to and I can console dot log the
new array it's compact not concat
otherwise it won't do it so now I run it it just prints one into it removes all
the defaults policy values all right so that's about it folks in the next video
of the series we're gonna look at a very important module that ships with node
which is called file system you can do a lot of file manipulation so follow the
link at the end of this video and I hope you learned something from this video
and if you did please like subscribe don't forget to Like like subscribe and
provide a nice comment and thank you