1. Home
  2. Node.js
  3. Node.js Basics
  4. Node.js – NPM

Node.js – NPM

NPM is Nodejs Package Manager for the JavaScript modules. It containers a large number of packages useful for applications. You can easily search and install packages using npm command line utility in your project.

#1. NPM Version

Use the npm version comamnd to find the version of NPM and other libraries used.

npm  version  

{ npm: '5.5.1',
  ares: '1.10.1-DEV',
  cldr: '31.0.1',
  http_parser: '2.7.0',
  icu: '59.1',
  modules: '57',
  nghttp2: '1.25.0',
  node: '8.9.3',
  openssl: '1.0.2n',
  tz: '2017b',
  unicode: '9.0',
  uv: '1.15.0',
  v8: '6.1.534.48',
  zlib: '1.2.11' }

#2. Search a Nodejs Module

You can search any module on npmjs.org package repository from command line. Like search the mysql2 module.

npm search mysql2

npm search module

#3. Install a Nodejs Module

You can install Node.js module in two ways.

Local Install

This install module in node_modules directory under the current directory. This module will be accessible under current project only.

npm install express

Global Install

This install a module in node_modules directory under the global directory of Node.js.

npm install express -g

#4. Uninstall a Nodejs Module

You can simply uninstall any module from current project using the following commands.

npm uninstall module_name

#5. Using Module in Application

You can simply add the functionality of any module in your application using require. For example, adding the express module to our local server.

var express = require('module_name')

The sample HTTP server using express look like below. Create a express_http.js using the following content.

var express = require('express')
var app = express()
 
app.get('/', function (req, res) {
  res.send('Hello World')
})
 
app.listen(3000)

Now execute the following command to start the node.js server.

node express_http.js