Illuminatiiiiii
8/26/2018 - 12:22 AM

How to use NPM

NPM is simple but sometimes you forget ;{

NPM is a thingy that allows you to use thousands of different javascript(& node) api's made by random people. We already know how to start an npm "project" by doing npm init but with that, we can install whats called packages which are simply just the apis that you can use. To install a package you have to know the name of the package you want. The packages can be found on https://www.npmjs.com and you install them by running "npm install packagename". That will install the package just on your computer though, so if you add --save to the end of that command, it will save it to your package.json which is a list of all the saved packages your project depends on to run. Then, if you move your project to another machine, you dont need to bring all of the possibly thousands of project files, you only need to run one command and it installs all of the packages in your package.json file. Pretty cool. 

So, once a package is installed, you can't necessarily use it yet. You need to add the require statement to your project. An example will be shown directly below.
-----------------------------------------------
Now lets actually do a full example of using a package. We are going to use this package: https://www.npmjs.com/package/cat-me
This is a package that allows you to print out images of cats. How magical. Now, there's two ways to use this package according to the documentation below the package. It says we can use the method that comes with the require statement, or we can use it as a CLI, which is basically just a terminal command. Let's do both. First, let's try the CLI way. To do it the CLI way, we have to install it globally. To do that we can just do "npm install cat-me -g". Now its usable. run "catme" to print an image of a kitty. run "catme --help" to see a list of commands. So many kitties.
Now, lets do the important stuff. Lets figure out how to actually install the package into our app. Setup a basic application so that we can run it. Run it to make sure its actually working. Now let's install the package. Do "npm install cat-me --save". Then lets add our require thingy. The code will be shown below. Once you have setup your app, lets add the require statement. Look below to see what I added. It's standard for any package of course.
Now that we have done that, we can use the catMe const that we made to spawn kitties. Look at the code below, we called it as a method, catMe(); Add that and run the application and see what it does. You can even use parameters. Look at the code below. 

If you have any questions or edits I can add to this, leave a comment here or on my video. 
Video: 
const express = require('express');
const app = express();
const catMe = require('cat-me'); //notice how we used the package name cat-me exactly as it's named.

catMe();

//catMe("nyan"); spawns a nyan cat

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(3000, () => console.log('Example app listening on port 3000!'));
const packagename = require('packagename'); //The package name has to be exact, but the variable/const name can be anything. 

//This code will take the installed package and fit it into a variable. You can then use that variable to call upon the various methods/functions that might exist in your package. A lot of the time, packages will have descriptions that tell you exactly how to use the package. It's super simple.