6

What is Node Package Manager – NPM ?

[Social9_Share type="horizontal"]

What is NPM ?

Before jumping into creating a project, I will give a short explanation about NPM – Node Package Manager.
Node has got core modules like HTTP, TLS, HTTPS, File system, Datagram and NET.If you want to use the public modules from the Node community, you need to use NPM.

Node Package Manager

NPM is a powerful package manager. This maintains a powerful repository of public modules, available at http://search.npmjs.org . It provides a command line tool to download, install and manage the packages. NPM also provides a registry service where programmers can publish their modules.

Let us discuss how to install , uninstall, update modules and resolve dependencies between modules

Install a Module

To install a module from the command line, use

where package- name is the name of the module to be installed

where http-server is the package which I want to install.

The screen shot is given below.

npm-install

NPM creates a folder node_modules under the current directory, if it does not exist or else uses the existing one. In my case it is installed to D:/UI technologies/Node projects/ServerDemo/node_modules
NPM, then downloads and installs this module in node_modules folder. You can choose the location to store your packages by using the modes of NPM

Modes of NPM

NPM has two modes – local and global.
The local mode is the default mode of NPM and the module is installed inside node_modules in the current directory.

The global mode is used if the package/module is needed globally. If node is installed in the default directory, the packages in global mode will be installed in C:/Users/local/lib/npm/node_modules

To install a module in global mode using npm use -g flag.

Uninstall a Module

To uninstall a package, use

To uninstall a globally installed package use

Now http-server will be uninstalled from the system.

Update a Module

To update an installed module to the latest version, use

If package does not exist, it will install it.
To update a globally installed module, use

List the Module

If you want to know the modules that are installed in your project, use

and for global access use

npm-list1

To know the version of the module installed use

will return the modulename @ version
you can refer the screen shot below



npm-list3

Resolving Dependencies

NPM installs all dependent packages, while installing the main module.

eg.
you request to install package Test1 & this depends on package Test2 and Test3.Node will fetch packages Test2 and Test3 and install them inside ./node_modules/ Test1 /node_modules folder.

In case of http-server, the dependencies like opener,corser, http-proxy and others are installed automatically.
NPM creates node_modules inside http_server and installs the dependencies.

The below screen shots show that.

npm-dependencies1

npm-dependencies

So, now my http-server is installed. The next step is to start the server.

Start the http-server

To start and run the http-server, navigate to http-server/bin folder under node_modules from command line.

In my case it is D:/UI technologies/Node projects/ServerDemo/node_modules/http-server/bin
Then type http-server

npm-server
Thats it. See – my http-server has started successfully.

Try this and keep your server ready. In the next session, lets try creating a Node Project.

Till then,
Happy Coding.

Comments(6)

  1. Reply
    anirvesh says

    nice article

    • Reply
      admin says

      Thank you.

  2. Reply
    shouvik says

    Thanks for the very clear tutorial. If newbies have any trouble, start the server with a “node”. So it’s “node http-server”… “http-server”.

Post a comment