Node Package Manager Tutorial How To Install And Uninstall Global And Local Packages And How To List Them

in #utopian-io6 years ago (edited)



Image Source

What Will I Learn?

I will learn how to install the local and global packages with the dependencies and dev dependencies , also how to uninstall and list them .

  • How to install the local packagaes with the dependencies and the dev dependencies .
  • How to install and uninstall the global packages with the -g flag
  • How to list the global and local packages and how to control them .

Requirements

  • Must downloaded the node js .
  • Basic knowledge about the command line .
  • Basic knowledge about modules in Node Js.

Difficulty

  • Basic

Tutorial Contents

In this tutorial we will talk again about the Node Package Manager ( NPM ) , in the first tutorial I have explained how to get the NPM , the help of this project and how to install the default package.json , in this tutorial I will continue my serie and I am going to show you how to install a local , global packages and how to uninstall them , finally how to listing packages , so let's start ..

Firstly why we install the packages ?

As I mentioned in the last tutorial the benefit of packages and why we use them , is to facilitate your work , the idea is when you need to a function for example to do something , you can almost find this code in a module or package you can just reuse it by installing this package and use it .

There is two types to install the packages , we have the local packages and the global packages , there is a simple difference between them , let's understand them ..

Local Packages

The terms ' local ' means you will install this package locally in this project in each project we have for example a folder has the ' package.json ' file , if we install locally the pacakge in this folder it will create another folder in your directory named ' node_modules ' , If you have another project, you will need to restart the command to install it again for this other project. This allows you to use different versions of the same module depending on your projects.
Am going to install my package.json file in my folder by the command npm init of course after chosing the directory

capture-20180411-122841.png

So I have create my package file as I explained in the last tutorial and this is my folder , I want just to give you the correct elements in your folder

capture-20180411-122736.png


There is many modules or packages on npm I want to install one of theme locally for example I will install the ' markdown ' package to use it on my node js file this is the simple code npm install markdown but be curfel you must choose the same directory of your node js file to include it by the var markdown = require('markdown') in your node js file , I will install it now ..

capture-20180411-124129.png

The npm has installed 3 modules in a local folder named ' node_modules ' as I said and it installed also another pacakge.json file named ' package-lock ' it came with the markdown module and this is my folder

capture-20180411-124936.png

And if I run the node_modules file this is the result :

capture-20180411-125415.png


This is our module that we have installed locally , this is a version only in this project you can install anohter version on another application .

The package.json file is the ID of our packages we can make a package dependency to become in our ID , firstly this is our package.json file :

capture-20180411-144039.png


If you look at the dependencies array it's empty though we have our module , now I will make the markdown module a dependency I need to type this command npm install markdown --save like it and before that I deleted the node_modules folder to be correct

capture-20180411-144655.png

The same installation but now if we open our package.json file this is the content

capture-20180411-144800.png

We have this code
"dependencies": {

"markdown": "^0.5.0"

}


In our dependencies propriety I have the ' markdown ' module with the version '0.5.0 ' , it means that we have a dependency now in our markdown library .
Let's explain now actually what's the difference between the dependency and dev dependency ..

The dependency is just to use the package by the command npm install package --save but the dev dependency is to develop a package you need to download and develop this package to your project you need to install it with the flag --save-dev to be a dev dependency to the products ..

I will give you an example how to install it and how we can find the dev dependencies in our project , firstly let's install a package --save-dev

![capture-20180411-150536.png](

I installed a moment package which is a javaScript date library for parsing, validating, manipulating, and formatting dates , let's see our package.json file

capture-20180411-150803.png


Another propriety named ' devDependencies ' has the library that I want to develop , and like it you can install the local packages .

I need now to uninstall these packages from my command line simply with this code npm uninstall markdown--save the same to install or to uninstall packages if it's dependency you need to add the ' --save ' and this is the result

capture-20180411-151730.png

It has removed 3 packages , I will see my package.json file now

capture-20180411-151827.png


The dependencies array is empty now , so the package was removed from our folder , I need the same thing to uninstall the moment dev dependency package I must add the flag -dev and this is the code npm uninstall moment --save-dev

capture-20180411-152911.png


it has removed 1 package which is the moment dev dependency and now I want to see again my package.json file and this is the result

capture-20180411-154600.png

So the devDependencies array also is empty now , we have uninstalled them by the command line by this code npm uninstall package and if it's a dependecy just make the --save if it's dev dependency package --save-dev .

Global packages

The term global means that's global anywhere for everything , if we apply it here we get the idea that the packages are globally installed on your folder and you can get it anywhere in any project or application that you are working , it's binaries in the path environment variable , to install the package globally you need to type this code npm install markdown -g and this is the result

capture-20180411-160255.png


If we look to our node_modules folder this is the result

capture-20180411-161209.png

There is nothing in our node_modules folder and this is the global packages if we install it nothing will be in our locally folder , if we look at the package.json file this is the result

capture-20180411-161536.png

The same thing here we have nothing in our package.json and this is the difference between the global and local installing , the global unregistered but the local has 3 types and registered in the node_modules folder .

How to uninstall the global packages ?

Simply with this code npm uninstall moment -g we specify the node package manager and the command uninstall then the package with the flag ' -g ' to be globally and this is the result

capture-20180411-161946.png

1 package removed globally from our device , by the global option you can use the moment package anywhere .

Listing Packages

After installation you can list the packages to look at the global packages and the local packages and for that we need to type this command npm list and this is the result of this command


capture-20180411-164703.png

I have 3 packages ' moment , loadash and markdown ' the markdown is a global package and when we type ' npm list ' it will give us just the locally packages and to list the global packages we need to add this flag --global true so you need to activate the global option like a true value and this is the result

capture-20180411-165743.png

So I have the moment package inside it as a dependency we have other packages , to list just the global package we must include another option ' --depth 0 ' and this is the result

capture-20180411-170122.png


And if you look at the command we have a directory where our module installed ' C:\users\qy\AppData\Roaming\npm ' and by dir this is the result

capture-20180411-170517.png

This is the way how to list your global and local packages and you can control the depth of the packages .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

I know just posting thank you is frowned upon in some situations, but I just started following you and had to acknowledge the usefulness of this post and the details on npm. I will explore your blog for other postings and tutorials as soon as I get the time. Do you have or know of tutorials on setting up and using git?

Congratulations @alexendre-maxim! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.

To support your work, I also upvoted your post!
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Thank you for the contribution It has been approved.


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Hey @alexendre-maxim I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Utopian Witness!

Participate on Discord. Lets GROW TOGETHER!

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63837.42
ETH 2539.78
USDT 1.00
SBD 2.65