NPM stands for node package manager. It is a package manager for node.js applications. It helps you to install , update, remove and publish packages. Node.js package ecosystem is the world's largest ecosystem of open source online libraries. We never have to write a already existing code again which reduces the efforts of the programmers upto a large extent and they can focus on solving the unsolved problems instead of rewriting the same code again and again.
Installing npm automatically :
NPM is installed automatically by downloading node.js. You can donwload node.js
here .
Installing npm manually :
We can also install npm manually. You can get the file from the link
given below :
https://registry.npmjs.org/npm/-/npm-{version}.tgz
All you have to do is change the version number just like the example given below :
https://registry.npmjs.org/npm/-/npm-5.4.2.tgz
Note : Not for noobs.
NPM updates comes more frequently as compared to node.js and we can't download node.js again and again just to
update npm. So we have to make sure that we are using the latest version of npm .
We can update a npm using the following command :
>npm install npm@latest -g
>npm -v
package.json
is a way to manage the locally installed packages.
package.json
serves as documentation on which our project depends.
npm init
command.
The snippet is given below :
>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (nodejsera)
version: (1.0.0)
description:
entry point: (arrays.js)
test command:
git repository:
keywords:
author: @rajatgarian
license: (ISC)
About to write to \package.json:
{
"name": "scripts",
"version": "1.0.0",
"description": "",
"main": "arrays.js",
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"ejs": "^2.5.7",
"express-fileupload": "^0.1.4",
"nodemailer": "^4.1.0",
"natural": "^0.5.4",
"prettyjson": "^1.2.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "@rajatgarian",
"license": "ISC"
}
Is this ok? (yes) yes
npm init --yes
If we are having a package.json
file in our project , then we can install all our
dependencies using just npm install
command.
Example for the same is given below ::
>npm install
In npm , there are 2 ways in which we can install our packages :
require
them in our application for example express, gannit , etc.
The syntax for downloading a package locally is :
npm install < package_name >
.
>npm install gannit
node_modules
( only if it is not yet created ) and download the package in that directory.
npm install
worked or not by manually checking whether the
directory with the name of node_modules
exists or not . And secondly if node_modules
directory exists , does it contain a directory for the package we installed.
npm install -g < package_name >
.
>npm install -g forever
package_name
with @
character.
The Syntax is npm install package_name@version
.
The snippet is given below :
>npm install express@4.15.1
npm install package_name@^major_version.0.0
.
The snippet is given below :
>npm install express@^4.0.0
npm update
.
>npm update
package.json
file.
npm install
worked or not by running the command
npm outdated
command. If there is no output on the console , then it means all the packages are
updated.
>npm outdated
npm install -g < package_name >
The snippet is given below :
>npm install -g forever
npm outdated -g --depth=0.
The snippet is given below :
>npm outdated -g --depth=0
Package Current Wanted Latest Location
@angular/cli 1.1.3 1.4.2 1.4.2 xyz
babel 6.5.2 6.23.0 6.23.0 xyz
babel-cli 6.18.0 6.26.0 6.26.0 xyz
nodemon 1.11.0 1.12.1 1.12.1 xyz
sails 0.11.0 0.12.13 0.12.13 xyz
webpack 1.13.3 3.6.0 3.6.0 xyz
webpack-dev-server 1.16.2 2.8.2 2.8.2 xyz
package.json
that satisfies semver
. It shows the
currently installed version of the package if we have no semver
i.e.
we are running npm outdated global -g , or the package is not included in package.json.
npm update -g
In npm , there are 2 type of package installation i.e locally and globally. So , Let's see how we can remove local and global packages one by one.
npm uninstall < package_name >
.
>npm uninstall gannit
package.json
by using the following command :
>npm uninstall --save gannit
devDependency
then we can remove it
using the following command :
>npm uninstall --save-dev gannit
node_modules
directory.
npm uninstall
worked or not by manually checking whether the
package exists in the node_modules
directory or not. If the package doesnot exist then it
signfies that the command is executed successfully.
npm uninstall
command globally.
The syntax for global download of packages is :
npm uninstall -g < package_name >
.
>npm uninstall -g forever
In this article , we learned about the basics of npm. How we can install packages from npm locally and globally . Updating local and global packages using npm. Removing local and global packages.