Node Package Manager is a package manager that can be used for the javascript programming language and contains all the files you need for a module. It is the default package manager for the JavaScript runtime environment. There are also many packages which add commands for you to use in the command line. It can manage multiple versions of code and is used on the server side.

Node Package Manager provides command line utility to install node.js Package and can get easily installed on any Computer. There are two types of npm package in node.js :

  • Local Package
  • Global Package

Local Package

  • If you want to depend on the package from your own module, using something like Node.js’, require then you want to install locally. This is npm is the default behavior.

Install Local Package

A package can be downloaded with the command

  npm install <package_name> 

This will create the directory innode_modules your current directory and will download the package to that directory.

Update Local Package

To update the packages of your application you need to do this:

Run npm update in the same directory

C:\Users\Magnet Brains\Desktop\node-app>npm update
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Magnet Brains\Desktop\node-app\package.json'
npm WARN node-app No description
npm WARN node-app No repository field.
npm WARN node-app No README data
npm WARN node-app No license field.

+ [email protected]
+ [email protected]
updated 2 packages in 2.816s

To run npm outdated

C:\Users\Magnet Brains\Desktop\node-app>npm outdated
Package       Current  Wanted  Latest  Location
es6-promise     3.3.1   3.3.1   4.2.2
mongodb-core   2.1.17  2.1.17   3.0.1

Install global package 

A global package is a command line tool. If you want to install a global package, it will work, no matter which directory is your current directory.To download global package, use the command prompt and type the command as

npm install -g  <package>

See the example –

npm  install -g hello

Hello is a package name

C:\Users\Magnet Brains\Desktop\node-app>npm install -g hello
C:\Users\Magnet Brains\AppData\Roaming\npm\hello -> C:\Users\Magnet Brains\AppData\Roaming\npm\node_modules\hello\cli\index.js
npm WARN [email protected] requires a peer of glob@* but none is installed. You must install peer dependencies yourself.

+ [email protected]
added 262 packages in 38.382s

 

Update a global package

The update a global package by using the command: npm update -g <package>

For example to update a package called  hello

npm update -g hello
Updated all  global packages
npm update -g

Version Check of NPM

The npm version bundled in the node.js installable various type of version. If we check the version  by opening the  command  prompt and type  the command as:

 npm  version  
C:\Users\Magnet Brains\Desktop\node-app>npm version
{ npm: '5.6.0',
  ares: '1.13.0',
  cldr: '32.0',
  http_parser: '2.7.0',
  icu: '60.1',
  modules: '59',
  nghttp2: '1.25.0',
  node: '9.3.0',
  openssl: '1.0.2n',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.18.0',
  v8: '6.2.414.46-node.15',
  zlib: '1.2.11' }

C:\Users\Magnet Brains\Desktop\node-app>

If you are working on an old version of npm then it is easy to update the latest version. Check the  latest  version  of  npm  again by  using  the  command  prompt and  type the command as:

npm  install npm -g

The syntax for –  install any Node.js module:

npm install <Module Name>

Following example is the command to install a famous Node.js web framework module i.e. called express

npm install express
C:\Users\Magnet Brains\Desktop\node-app> npm install express
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Magnet Brains\Desktop\node-app\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Magnet Brains\Desktop\node-app\package.json'
npm WARN node-app No description
npm WARN node-app No repository field.
npm WARN node-app No README data
npm WARN node-app No license field.

+ [email protected]
added 46 packages in 8.995s

Uninstalling modules

The uninstall modules then using the following command that is define –

npm uninstall express

C:\Users\Magnet Brains\Desktop\node-app>npm uninstall express
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\Magnet Brains\Desktop\node-app\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Magnet Brains\Desktop\node-app\package.json'
npm WARN node-app No description
npm WARN node-app No repository field.
npm WARN node-app No README data
npm WARN node-app No license field.

removed 1 package in 1.425s

The node.js module is uninstalled, then check the command:  npm ls

create module –

These packages are required for the package.json, generate a package.json using NPM, the following command are used for –  npm init

The result is:

C:\Users\Magnet Brains\Desktop\node-app>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 <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ctrl+c  at any time to quit.
package name: (app)

Learn More-