Node Package Manager Tutorial Updating Packages , NPM Prune , Shortcuts and NPM Scripts

in #utopian-io6 years ago (edited)



Image Source

What Will I Learn?

I will learn how update packages locally and globally and how to update the npm version , what's the npm prune and how to use the shortcuts finally what's the npm scripts .

  • How to update the packages globally and locally and how to update the npm version .
  • How to remove the packages by the npm prune .
  • What's the shortcuts and how to use them to facilitate the implementation
  • How to execute the node application from the npm .

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 I will talk about updating packages using the command line , the npm prune , the shortcuts and finally the npm scripts , so let's start ..

Updating Packages

Sometimes we need to update the version of our package , if you are working on a project with Node Js framework and you are using a package to help you to reslove your problems without effort , and when the developer of this package make an update for more options you need to update your package to get the new features .

To update the packages we have many forms to update a specific package or to update a global package ..etc let's understand them .

Firstly when you have installed many package and you need to update just one of them for that we will apply the first case , I need to install a package with a specific version as I explained in the last tutorial to update it , let's install the moment library npm install [email protected] --save as a dependency

capture-20180415-145806.png

I have installed the 2.21.0 version and I specific this version just to apply the update and this is my package.json file

capture-20180415-145833.png

And now let's update the moment package to update it we need to type this command npm update moment --save I selected the moment package as dependency , by this command you will get the last version of this package , let's execute it

capture-20180415-151713.png

And now to confirm by the version let's see our package.json file we can find the moment package in the dependencies array

capture-20180415-151750.png

As you see here the npm has updated the moment package to the last version , the same code if there is a package installed simply not as a dependency by this code npm update markdown and it will be updated .

The second case is to update all the packages in the dev dependencies array and to do that I will firstly install the library date for the version 1.0.1 as a dev dependency npm install [email protected] --save-dev

capture-20180415-154413.png

Let's see our package.json file to confirm by this version because I will select all the dev dependencies
capture-20180415-154814.png


I want now to update it as I mentioned by this code npm update -dev --save-dev

capture-20180415-155509.png

As you see here the date package has updated to the last version , here I have just a one package if you have other packages all of them will be updated using the code npm update -dev --save-dev .

In the third case if you want to update all the packages that you have installed you can use this code npm update , if we type this code nothing will be done because all the packages are in the last version I will change them ..

capture-20180415-160214.png

I have changed the versions to apply this command , let's type it and see what's happened

capture-20180415-160651.png

It has updated 2 packages the date and the moment to the versions 1.0.2 and 2.22.1 .

The fourth case is to update a package installed globally and to update it we need to add the -g flag to be like it

capture-20180415-161011.png

This command will update all the packages installed globally but if you want to specify a package just add the name of the package after the -g flag to be like it npm update -g package .

The final case if you want to update your version of npm you must firstly run the command line with administrative privilege then type this code npm install npm@latest -g and this is the result

capture-20180415-161933.png

As you have seen the npm has been updated and there is 523 packages also updated , and now let's see our npm version by the code npm -v

capture-20180415-162711.png

So our npm has updated from 5.6.0 to 5.8.0 there is new feautues and options that we will descover together ..

NPM Prune

As I explained in the last tutorials how to install the packages locally and globally and what's the dependencies and dev dependencies , there is a concept for the extraneous packages which are not important in my project , what I mean by not important ?


These packages are not important because they are not installed in the dependencies and what we need really is the dependencies packages , let's see an example to understand what I mean

capture-20180415-164850.png

I have the package markdown installed in my application but it's a extraneous package if you look at the dependencies in my package.json file

capture-20180415-164918.png

Now let's list our packages by this code npm list --depth 0 to give us just the principal packages and this is the result

capture-20180415-165518.png

There is two packages ' date and moment ' are important but there is a package with a green mark ' extraneous ' and there is an error the markdown is a extraneous package , it's not important how to remove it ?

To remove these packages we need to use the npm prune , this command will remove all these packages and give you just the important packages that you used in your project and this is the code npm prune

capture-20180415-170402.png

The npm has removed the packages without problem and to confirm let's list the packages again

capture-20180415-170619.png


Now the list has just the moment and date packages and no extraneous package existing now in my project .

Shorcuts

The terms shortcuts varies from one domain to another for example here the shortcut is to cut the word and use a part of it let's understand it with examples

For example to create a package.json file we have used this code npm init --yes when we apply the shortcuts it will be like it

capture-20180415-171611.png

The same result when we type npm init -y , if we want for example to install a package locally we type just npm i moment in a place of npm install moment --save and it give us the same result

capture-20180415-171926.png

And to install the package as dev dependencies , the simple code is npm install markdown --save-dev by using the shortcodes it will be npm i markdown -D
capture-20180415-172739.png

NPM Scripts

Let's give a helpful example to understand what are the scripts and how to use them , in my package.json file I will replace the propriety test in the scripts array by ' start ' and I created a node js application named pro.js I will run this application by writing ' node pro.js ' like it

capture-20180415-173425.png

Inside my pro.js file I have just a console.log like it console.log("Welcome to this Script from the npm , utopian io"); and I will run the npm by npm start and this is the result

capture-20180415-173554.png

The npm has executed the pro.js application from the start propriety in the scripts array and it print on the console the sentence that I have created in my node application .

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for your contribution, but it cannot be accepted due to several reasons such as:

  • Your content is already heavily available and well documented on npmjs documentation. Check out for example here https://docs.npmjs.com/cli/update and https://docs.npmjs.com/cli/prune
  • Your content is sometimes incomprehensible. I've had difficulty understanding your language and following up your ideas. Please make sure to improve your language structure and ideas for future contributions.

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

[utopian-moderator]

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63248.94
ETH 2576.33
USDT 1.00
SBD 2.85