NodeJS_unit_3

3.1 What is NPM ?

NPM is a short form of Node Package Manager, which is the world's largest software registry. The open-source web project developers use it from the entire world to share and borrow packages. The npm also acts as a command-line utility for the Node.js project for installing packages in the project, dependency management,and even version management.

Components of npm

NPM mainly consists of three different components, these are:
1. Website: The npm official website is used to find packages for your project, create and set up profiles to manage and access private and public packages.
2. Command Line Interface (CLI): The CLI runs from your computer's terminal to interact with npm packages and repositories(sources).
3. Registry: The registry is a large and public database of JavaScript projects and meta-information. You can use any supported npm registry that you want or even your own. You can even use someone else's registry as per their terms of use.

Installing npm (Node.js and npm)

npm comes with Node.js, which means you have to install Node.js to get installed automatically on your personal computer. There are two different methods to install npm on a computer.
1. Using nvm (Node Version Manager)
2. Using Node installer
npm installation is highly recommended using nvm (Node Version Manager) rather than using Node installer.

Need For NPM

• Adapt packages of code for your apps.
• Download standalone tools you can use right away.
• Share code with any npm user, anywhere.
• Restrict code to specific developers.
• Create Orgs (organizations) to coordinate package maintenance, coding, and developers.
• Form virtual teams by using Orgs.

Advantage of NPM

• It is free to use and open source package system.
• It is simpler than SOAP.
• It is default package Manager for NodeJS.
• Completely open source and written in JavaScript.
• As it manages all the package and modules for NodeJS and it consists of also command line NPM, so it’s become world’s largest software industry.

3.2 Installing Packages Locally

• There are two ways of installation the package either locally or globally, when should we install package globally or locally question arise, answer based on this question is how actually we want to use those package.
• We can download a package with command: npm install <package_name>
• This command creates node_module directory in your current directory downloads the package to that directory.
e.g. npm install express
• If Package.json file doesn’t exist in the local directory, install latest version of the package.

Using the installed package in our code:

• Once we install package so we can use this package in our code. For that require() method is use.
• e.g.
//index.js
    var express=require(‘express’);
    app.use(express)
    console.log(“Successfully required a package”);
• if we hadn’t installed express package properly we get “cann’t find module ‘express’ “error message

How to uninstall Local Package

• If we want to remove package or module directly from our node_module directory we should use:
Syntax: npm uninstall <package_name>
e.g.: npm uninstall demo
• If we want to remove it from the dependencies in package.jason, we should use the save flag:
npm uninstall –save demo

3.3 Adding dependency in package.json

What is the package.json file?

• It is one of the most important files when working with node application. With this single file we can track of all our external dependencies.
• All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.
• It can also contain other metadata such as a project description, the version of the project in a particular distribution, license information, even configuration data - all of which can be vital to both npm and to the end users of the package.
• We can add dependencies to a package.json in two ways:
1. File from the Command Line.
2. Manually editing the package.json file.


Adding dependencies to a package.json file from the command line

To add dependencies and devDependencies to a package.json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install) or the --save-dev flag for devDependencies.
To add an entry to the "dependencies" attribute of a package.json file, on the command line, run the following command:
npm install <package-name> [--save-prod]
To add an entry to the "devDependencies" attribute of a package.json file, on the command line, run the following command:
npm install <package-name> --save-dev

Manually editing the package.json file

To add dependencies to a package.json file, in a text editor, add an attribute called "dependencies" that references the name and semantic version of each dependency:
    {
    "name": "my_package",
    "version": "1.0.0",
    "dependencies": {
    "my_dep": "^1.0.0",
    "another_dep": "~2.2.0"
    }
    }

To add devDependencies to a package.json file, in a text editor, add an attribute called "devDependencies" that references the name and semantic version of each devDependency:
    "name": "my_package",
    "version": "1.0.0",
    "dependencies": {
    "my_dep": "^1.0.0",
    "another_dep": "~2.2.0"
    },
    "devDependencies" : {
    "my_test_framework": "^3.1.0".
    "another_dev_dep": "1.0.0 - 1.2.0"
    }

3.4 Installing packages globally

We can use command npm install –g , for install:
npm install –g <package>
if we want to uninstall a package called jshint we would type:
npm uninstall –g jshint

3.5 Updating packages

Updating local packages

We recommend regularly updating the local packages your project depends on to improve your code as improvements to its dependencies are made.
1. Navigate to the root directory of your project and ensure it contains a package.json file:
cd /path/to/project
2. In your project root directory, run the update command:
npm update
3. To test the update, run the outdated command. There should not be any output.
npm outdated

Updating globally-installed packages

Note: If you are using npm version 2.6.0 or less, run this script to update all outdated global packages.
However, please consider upgrading to the latest version of npm:
npm install npm@latest -g
Determining which global packages need updating
npm outdated -g --depth=0
Updating a single global package
npm update -g <package_name>
Updating all globally-installed packages
npm update -g

No comments:

Post a Comment