Thursday, April 2, 2020

What is NPM??

NPM stands for Node Package Manager and is a software registry for javaScript packages. NPM comes "packaged" with NodeJS, so you have to install node before you can use npm.  Once node is installed npm comes with its own CLI (command line interface) we will use to access, manage and install 3rd party packages we will need for our project.

If we are creating a new project, we will need to initialize the project by typing the init command below into the command line terminal in the root location of our project.




 $ npm init


This will walk us through the process of creating a package configuration file. This file holds all the configurations for our new node project. Below is an example of how the file will look after you have completed the npm init process



{
"name": "project-name", // can be anything
"version": "1.0.0", // version of our project
"description": "Lorem ipsum dolor sit almet...", // can be anything
"main": "index.js", // entry point of the project, how npm imports the project
...
"repository": {
"type": "git",
"url": "http://www.github.com/" // url to our repo where the code lives
},
"author": "Jacob King", // your name, alias, or organization
"license": "MIT" // the license your code falls under
}
view raw package.js hosted with ❤ by GitHub
A full rundown of each part of this file, check out package.json.  Once we have the project initialized, we can start adding packages to it. We do that by "installing" the package using the cli.

$ npm install <package-name>

This will create a folder in the root directory called "node_modules" that will contain the newly installed package and all of it's dependencies.

I do want to take a moment and mention you can install a package globally on your machine where it can be used with any project an instance where you might want to do this is for installing a simple http server for running your projects locally.

$ npm install -g <package-name>

So here is where we come to a problem, how do install multiple dependencies at one time. We could list them out one after another in the npm command, but there is an easier way to do this. Instead of just installing the packages, lets add them as dependencies to our project. We do this with the following command:

$ npm install <package> --save

This --save tag adds the package as a dependency in the package.json file we created before. We can do this for all our packages. Now that they have been recorded into the package configuration file. when we clone the project in the future all we have to do is run:

$ npm install

Now, all package in our configuration file (package.json) will be installed simultaneously. There is obviously a lot more to npm such as you can upload your custom packages to their registry to be used in other projects, I will leave a couple of good links so you can read more about all the capabilities of npm.

Now become the king of your world!!

https://www.w3schools.com/whatis/whatis_npm.asp
https://docs.npmjs.com/
https://youtu.be/jHDhaSSKmB0

No comments:

Post a Comment