Managing third party packages with npm

Managing third party packages with npm

As we have seen, the “Node package manager” has the ability to manage modules, which are required by Node.js applications.

Let’s look at some of the functions available in the node package manager for managing modules

1. Installing packages in global mode – Modules can be installed at the global level, which just basically means that these modules would be available for all Node.js projects on a local machine. The example below shows how to install
the “express module” with the global option.

npm install express –global

The global option in the above statement is what allows the modules to be installed at a global level.

2. Listing all of the global packages installed on a local machine. This can be done by executing the below command in the command prompt

npm list –global

3.Installing a specific version of a package – Sometimes there may be a requirement to install just the specific version of a package. Once you know what is the package and the relevant version that needs to be installed, you can use the
npm install command to install that specific version.

The example below shows how to install the module called underscore with a specific version of 1.7.0

npm install underscore@1.7.0

4.Updating a package version – Sometimes you may have an older version of a package in a system, and you may want to update to the latest one available in the market. To do this one can use the npm update command. The example below shows
how to update the underscore package to the latest version

npm update underscore

5.Searching for a particular package – To search whether a particular version is available on the local system or not, you can use the search command of npm. The example below will check if the express module is installed on the local
machine or not.

npm search express

6.Un-installing a package – The same in which you can install a package, you can also un-install a package. The uninstallation of a package is done with the uninstallation command of npm. The example below shows how to uninstall the express
module

npm uninstall express

What is the package.json file

The “package.json” file is used to hold the metadata about a particular project. This information provides the Node package manager the necessary information to understand how the project should be handled along with
its dependencies.

The package.json files contains information such as the project description, the version of the project in a particular distribution, license information, and configuration data.

The package.json file is normally located at the root directory of a Node.js project.

package.json

                    
    {
        "name": "node-crud",
        "version": "0.0.0",
        "private": true,
        "scripts": {
            "start": "node ./bin/www"
        },
        "dependencies": {
            "body-parser": "~1.16.0",
            "ejs": "~2.5.5",
            "express": "~4.14.1",
            "mongoose": "^4.8.4"
        }
        }                             
                    
                

Node Commands

  • Cmd 1 : nvm setup executed

  • Cmd 2 : C:\Users\user_name>nvm -v //Running version 1.1.6.

    NVM Usage :

    1. nvm list -> List installed node versions
    2. nvm on
    3. nvm off
    4. nvm uninstall[version]
    5. nvm use[version]
    6. nvm root[path]
  • Cmd 3 : Check Node Version

    C:\Users\user_name>node -v //v6.11.2

  • Cmd 4 : Check npm Version

    C:\Users\user_name>npm -v //6.0.0

  • Cmd 5 : list npm user-installed packages

    npm list -g –depth=0

        Ex : 
        @angular/cli@1.7.3
        +-- express-generator@4.16.0
        +-- gulp@3.9.1
        +-- jade@1.11.0
        +-- jshint@2.9.5
        +-- npm@5.10.0
        +-- nvmw@0.2.1
        +-- typescript@2.2.2
        +-- update-node@0.1.0
        `-- webpack@3.6.0
            
  • Cmd 6 : Try To Run Angular/cli

    C:\Users\folder_name>npm install -g @angular/cli

  • Create New Project

    c:\Train>ng new MyFirstApp

    You are running version v6.11.2 of Node.js, which is not supported by Angular CLI v6.

    The official Node.js version that is supported is 8.9 and greater.

  • Cmd 7 : Update npm package

    c:\Train>npm install -g npm //npm@6.2.0

  • Cmd 8 : npm Updated , Verify with cmd

    c:\Train>npm -v //6.2.0

  • Cmd 9 : Try to install angular/cli with version

    c:\Train>npm install -g @angular/cli@1.7.3

    Successfully Installed : + @angular/cli@1.7.3

    added 586 packages from 565 contributors, removed 26 packages and updated 20 packages in 285.024s

  • Cmd 10 : Verify angular version

    c:\Train>ng -v

        @angular/cli: 1.7.3
        node: 6.11.2
        os: win32 x64
            
  • Cmd 11 : Uninstall Angular Cli

    npm uninstall -g @angular/cli@1.7.3

  • Cmd 12 : Create new project

    c:\Train>ng new MyFirstApp

        .......
        Installing packages for tooling via npm.
        Installed packages for tooling via npm.
        Project 'MyFirstApp' successfully created.
                
  • Cmd 13 : Test project from cmd

    c:\Train\MyFirstApp>ng serve //Output in browser : http://localhost:4200/

  • NPM Package

Summary

  • A module in Node.js is a logical encapsulation of code in a single unit. Separation into modules makes code more manageable and maintainable for future purposes
  • There are many modules available in the market which can be used within Node.js such as express, underscore, mongoDB, etc.
  • The node package manager (npm) is used to download and install modules which can then be used in a Node.js application.
  • One can create custom NPM modules, extend these modules and also publish these modules.
  • The Node package manager has a complete set of commands to manage the npm modules on the local system such as the installation, un-installation, searching, etc.
  • The package.json file is used to hold the entire metadata information for an npm module.

Leave a Reply

Your email address will not be published. Required fields are marked *