In this video I want to look at the node package manager.
What is the node package manager what is a Node package? Why would you want to
manage packages?
who is managing them for you what all that sort of stuff, so what is this thing?
why do you need it? And why is it relevant in particular for this context
of building a Twitter bot with node?
Ok so up I'm gonna come over here to the whiteboard not really sure why just yet.
So node package manager is typically referred to online as NPM. If yougoogle npm
you'll probably find a node package manager website. This is not a separate
thing you need to install.When you install node NPM came with it and npm
is another command that you can run from the terminal from a console on and execute
various things associated with the project.
So the the first thing that we ...ok so let's think about so why this thing
exists anyway. So you're writing code,
you're a person who's working on something with code and you have this
idea because you want to work with an API or you want to generate SVG, PDF
images for some other thing right.
And you're working javascript in node and you're.. Well how do you do that
So it's very rare that you're just going to build everything from nothing. You're
just going to start from a total clean slate. All you have is javascript in
front of you and you're going to somehow build an entire engine that renders
PDFs or talks to York Times API.
Most likely you want to use somebody else's library of code that they made
available open source to you that you can kind of piggyback off.And
hopefully you might contribute back to it or make your own package of some day
but this is what a package is.So the particular package ... then I feel weird
saying because I don't know..
twit is like a weird thing to say. But the particular package that I'm going
to use in these particular videos about making a twitter bot is a package called
twit.
Because i am a twit
let's just all get that out there.
That's why am I am a twit I am NOT ah I always forget to reset these cameras.So
okay so I'm a twit for not resetting the cameras ok
I don't even know what that is what is a twit it's a it's what I want anyway.
Ok so a package and NPM. So how do you install a package : You say
npm install "the name of the package"
So where do you say that, why you, when do you say that how do you do that.So one
of the things that happens when you work on a node project is that you want to
have this thing that this file called "package"... as you saw you can just write
Javascript in the previous video you can just write so,e Javascript Code just script code
executes JavaScript code it works. But when you start to like think of a
project you're making that has a bunch of different components and imports
various packages and might depend on other modules can keep using all these
different kinds of words.This file called package.json is like ...you can
think of it as your "settings" file for your whole project or your "config" file
for your whole project.One of the things that you might put in a
package.json file is other types of other...
what's known as other dependencies dep- end and "deep n Dead Sea's" (nice one auto caption). Never got
close enough so.
Notice here before the file extension that JSON
this is a file that's of the JSON format. Magic link to video that i made about
JSON and what that is!
Make that appear in an annotation or a card or something I don't know.
So uh so that's so so you could start to write this file yourself . But one of the
things you can do with node package manager is say npm
in it so that's the very first thing that I think i'll demonstrate to you as
I come over here. So i'm going to minimize chrome and I'm going to come
over her. Now remember i'm gonna say ...By the way something I like to type is
"clear" which clears the terminal on a mac i'm going to say PWD
SoI'm in you know processing desktop youtube code this is the folder that I'm in.
And actually something i could do is say "open." . What that's going to do is
open that folder in the finder
so I want to kind of go back here and I'm going to make another folder called
node2And what I want to do is now cd .. it will move me up one directory so you
can see I'm just in the YouTube code directory. And now i'm going to go to cd
node2
- so now i'm in this folder right you can see it there there's nothing in
there and now is the time where we're going to say something exciting.I'm
going to say and npm init.
So with that what this does is it's telling you "this utility will walk you
through creating a package.json file it only covers the most common items and
tries to guess sensible defaults". so this is useful to you you don't have to do it
this way you could make the package.json file yourself but npm.initt will
kind of prompt you to cite to ask you some questions like what's the name of
your project
You know what let's call it npm demo ...by the way no spaces can go in
this name which will often trip me up because it won't tell you till the end
so npm demo the version like if I'm keeping track of versions of projects
whatever 1.0 sounds good , description I am demo demos them on demonstrating npm
entry . entry . this is a key question what's the name of the file that node
should trigger to run your program I called it hello.js before.You can
call it index.jsyou can call it server.js
you could call it BOTS.js let's call it BOTS .js because that's ultimately
what we're going to make eventually.
Although this isn't really a bot yet.
Test command ... whatever i don't know what that is
let's skip that git repository that's another topic for another time.I'm not
going to worry about a git repository right now.
Keywords : "rainbow"
"awesome" whatever "awkward" maybe it's more appropriate for me and the author is a
myself.
Ahatever not that none of this matters I.. you know this matters if you're old and
license you know eventually it's like saying "does this look right to you"
and I'm just going to say yes.
so what you're seeing here is it's creating that package.json file a lot of
that doesn't really matter to you right now it's kind of good practice to get
started with it because ultimately you might want to release this as a kind of
published project or open source it. Upload it to this thing called github
make it official, a node package that other people can use
there's lots of possibilities here so you're kind of getting off on the right
foot
by starting with this package.json file so now if i go into SUblime here and i'm
going to say file.open and i'm gonna i'm just going to open..One of the things
you can do, is you can open Sublime, Sublime by the way that's a text
editor that i'm using its not free although you can download kind of a free
version but you can use any text editor for the stuff that I'm doing...
um you know text edit will work for you.
So one thing you can do with Sublime is open a full directory and you can see
now here on the side here
I have kind of a directory structure and what I want to do is look under node2 and
you can see look at that that file didn't exist I didn't make that file
this file arrived because I ran npn in it so i can click on this file and
you can see there's there's the JSON file it's got the name, the versions
it's got this thing called scripts, got some keywords as an author but I get
acting out edit this file just, change it manually like "oh I wanted to have a period
here" that sort of thing.
So you now have a package.json file
ok so now I said that I wanted to use the twit package
So how do you find out what packages you want what you might want to use and how
do you install them? so i can go back to Chrome here.
Go back to a browser and i can i'm just going to google npm. and i'm going to
end up on the npm , npmjs.com website
so this is a website which has a collection of all of many comments of
all packages that you could possibly think of installing. For example one
the ones that I i hope to make some videos about at some point is this
package called express and this is what you really want to use if you want to
like make a quick web server. If you want to host some files if you want to kind
of spin up your own API, would like to get to
I would like to make up set of videos about using Expres.But we're here and
we're like okay well let's say I want to use something that's going to help me
talk to twitter so i could type twitter in the search and you can see
wow there's a lot... easy easy twitter twitter.js asked Twitter tokens
Twitter 1.1 Twitter and so this now becomes this is your life
Welcome to your life! You know i don't know maybe you should go do
something else like how do you pick which one what do you do?
So a lot of this is you know ask a friend maybe already made something with
twitter i could be your friend.I might tell you that I use that twit one
who seems to be working for me why don't you try it
but another thing you can do is kind of look at it.You can see if it has good
documentation when was the last time was updated like it added if the last time a
code update like if I look at this one
like soff twitter we can see here... there not really like a huge amount of
documentation was published a year ago 0. 0.1 is the latest release like i might
say I don't like this. Wait maybe it is a wonderful package I don't even know who
made it or why it's there but this is the kind of things you might want to
look for. And certainly clicking over to the github repository which is where
that source code you can find the source code for it and you can see here that
you know september 5th 2014
it's only had like four commits so maybe this isn't sort of actively maintained
project.
Um I'm going to look for the twit1 i'm just going to put in a twit here and I
think you can see here this is a twitter api for node.
It says REST - if you want to take a rest
but you might want to after watching this video and streaming so this is good
We're going to use this streaming feature of the API . What streaming with
the API? it's kind of exciting and you can see here is giving you up a little
message of how oh I'm standing in front of it i have this bad habit of how to
install it npm install twit
it's giving you some like example code of how to use it.
This so this looks like it's got pretty good documentation and information
so this is a good one to use and I'm gonna use it for my examples
so now how do you install it now?
We saw that on the page it said you could say npm install twit and this will
in fact install the twit package.
However what I might suggest to you that you do is also type in
- - save. what - - save as an argument to NPM install means is save a reference to
this particular package to your package.json file and so now if i hit
enter
some stuff is happening it's warning me because I didn't really fill
out everything in that NPM package its installed a whole bunch of thing time
like twit actually depends on all these other packages that it's using one's
called tough-cookie which I'm not tough cookie.
I thought ... I'll melt very easily you know give me a package that's not well
doesn't have good documentation and I'm you know weeping instantly but i don't
know i'm talking about that um so
so you can see and install that and now couple things one is you can see look at
this
dependencies right now right now in my in my package that JSON finally added
this so as this project moves around or gets uploaded and shared.
It will always remember that this package this project working on requires
this twit dependency.. Which will help it if you like
deploying to a server and the server is going to automatically install the
dependency for you.
All sorts of possibilities there why this might be useful and also another
thing you'll notice is that there is suddenly now ...remember I'm in node2
if I go into this folder there's suddenly in addition to the package.json
file
There's a node modules folder and that node modules folder you'll notice has
the twit module and that has examples library other modules it depends on test
the readme file. So all the stuff for that particular node module is now in
the local directory for your project.
So now we are ready now we're actually ready to write some code.
It's kind of been a long journey here of like first got installed node and you
got to learn with this npm thing is that you got it like do npm init and then
you gotta do npm install but all of this
it has a purpose. It has a greater purpose because we're gonna get to the
fun part of actually talking to Twitter posting to Twitter coming up with weird
strange things that you might do automatically with Twitter bot.So so if
you're following along
what I would make sure you can do now is make a package.json file run npm in it
run npm install twit -- save then you'll be ready for the next video we're going
to start actually adding the code for talking to Twitter
and then it stopped.