Codementor Events

What Is NPM (Node Package Manager)?

Published Apr 24, 2021Last updated Oct 20, 2021
What Is NPM (Node Package Manager)?

NPM stands for Node Package Manager. It is the Node.js default package manager and is completely written in Javascript. NPM is a command-line client for Node.js that manages all of the dependencies and modules. With the installation of Node.js, it is added to the system. Any required packages and modules in node projects are installed using NPM. A package contains all of the files required for a module, and modules are JavaScript libraries that can be included in a Node project based on the project's requirements.
It includes a large number of libraries that serve as excellent tools for Node.js developers, speeding up the entire application development process. NPM can use the package.json file to install all of a project's dependencies. It has the ability to upgrade and remove packages. Each dependency may specify a range of valid versions using the semantic versioning scheme in the package.json file, allowing developers to auto-update their packages while avoiding unwanted breaking changes.

Some basic commands:-

  • npm i <packageName> :- installs a local package.
  • npm i -g <packageName> :- installs a global package.
  • npm un <packageName> :- uninstalls a local package.
  • npm up :- updates the packages.
  • npm t:- runs the tests.
  • npm ls :- lists all the installed modules.
  • npm ll :- prints additional package information while listing modules.

Uses of NPM:-

  • It facilitates the integration of pre-built packages into our project.
  • It aids in the download of a number of standalone tools that can be used right away.
  • NPM is frequently used by developers to share their code with other NPM users all over the world.
  • It helps in managing and maintaining various versions of codes and their dependencies.
  • NPM automatically updates the application when the underlying codes are updated.
  • It functions as a community where you can connect with other developers who are working on similar projects or tasks.
    NPM Applications.png

Some terminologies before getting into the working of NPM:-

  • Package:-
    From the official documentation, a package is a file or directory that is described by a package.json file. A package must contain a package.json file in order to be published to the npm registry. Packages can be unscoped or scoped to a user or organization, and scoped packages can be private or public.
    A package is any of the following:
    a) A folder containing a program described by a package.json file.
    b) A zipped tarball containing (a).
    c) A URL that resolves to (b).
    d) A <name>@<version> that is published on the registry with (c).
    e) A <name>@<tag> that points to (d).
    f) A <name> that has a latest tag satisfying (e).
    g) A git url that, when cloned, results in (a).

  • Module :-
    From the official documentation, a module is any file or directory in the node_modules directory that can be loaded by the Node.js require() function.
    To be loaded by the Node.js require() function, a module must be one of the following:
    1.A folder with a package.json file containing a "main" field.
    2.A JavaScript file.
    Since modules are not required to have a package.json file, not all modules are packages. Only modules that have a package.json file are packages. In the context of a Node program, the module is also the thing that was loaded from a file.
    For example, in the following program:
    var req = require('request')
    We can say that the variable ‘req’ refers to the ‘request’ module.

  • Dependency hell:-
    Let us assume that there are three modules : X, Y and Z. X requires Y at v3.0, and Z also requires Y, but at v4.0. We can visualize this like so:

    image4.png

Let us assume that we have an application that requires both module X and module Z.

image1.png

Now, there will be a confusion as to which version of B is to be included. This is called Dependency Hell.
image5.png

Working of NPM version 2 :-

In case of Dependency Hell, as discussed above, instead of attempting to resolve module Y to a single version, npm version 2 puts both versions of module Y into the tree, each version nested under the module that requires it.

image3.png

Working of NPM version 3 :-

In npm3, dependencies are resolved in a different way than in npm2. Although npm2 installs all dependencies in a nested fashion, npm3 tries to avoid the deep trees and redundancy that such nesting causes. npm3 tries to accomplish this by flatly installing secondary dependencies (dependencies of dependencies) in the same directory as the primary dependency that requires them.

The following are the most significant differences:

  • The type (primary, secondary, etc.) of a dependency is no longer determined by its location in the directory structure.
  • The order in which things are installed will change the node modules directory tree structure, since dependency resolution is dependent on installation order.

Let us assume we have a module X. X requires Y. Now lets create an application that requires module X. On npm install, npm version 3 will install both module X and its dependency, module Y, inside the /node_modules directory, flat.
In npm version 2 this would have happened in a nested way.

image7.png

Let us suppose that we want to require another module, Z. Z requires Y, but at another version than X.

image8.png

Since Y v1.0 is already a top-level dependency, we cannot install Y v2.0 as a top level dependency. npm v3 handles this by behaving in a similar fashion to npm v2 and nesting the new, different, module Y version dependency under the module that requires it, that is, module Z.

npm2.png

Installation of NPM:-

To install NPM, it is required to install Node.js as NPM gets installed with Node.js automatically.
Versions of npm installed on the system can be checked using the following command: npm -v.
If the version is not the latest one, we can install it using the command: npm npm@latest -g

Creating a Node Project:-

To create a new node project, we use npm init in the folder in which we want to create the project. A number of questions like name, license, scripts, description, author, keywords, version and main file will be asked. After the project is created, a package.json file will be created in the project folder which confirms that the project has been initialised. Learn more on Node.js here.

Installation of Packages and Modules:-

Following the creation of the project, the next step is to add the packages and modules that will be used in the Node Project. Use the following code to install packages and modules in the project:
npm install package_name
Example:
To install the mongoose package in our project, we use the following command :
npm install mongoose
We can add an extra -g tag to the package installation syntax to make it global, that is, accessible by all projects in the system.

Controlling the versions of the packages to be installed:-

  • In the package.json file, specify the complete and exact version to install a package of a particular version.
  • To install any version above a given version, mention it like : “packageName” : ”^<version Number>″ in package.json file. The caret symbol (^) tells the npm to find a version greater than <version Number> and install it.
  • Mention "*" in front of the dependency or "latest" to install the most recent version of the package. This will locate and install the most recent stable version of the module.

Controlling where the packages and the modules get installed:-

We can add the –save flag to install a package while also saving it to the package.json file. Because the –save flag is set by default in the npm install command, it is equivalent to npm install package name.
–save-prod: Installing this package causes it to appear in Dependencies, which is also the default.
–save-dev: This package will appear in devDependencies and will only be used in development mode if you use it.
Simply type npm install in the terminal if there is already a package.json file with all the packages listed as dependencies. NPM will examine the package.json file and install all of the dependencies in the order in which they are listed in the file. When a Node project is forked and cloned, this command is usually used.
NPM installs dependencies in local mode (by default), to the node modules directory of the Node application folder. Use the npm ls command to see all the locally installed modules.

Uninstalling Packages:-

To uninstall packages using npm, type the command :
npm uninstall

To uninstall global packages, type the command:
npm uninstall package_name -g

Some Node.Js Frameworks:-

Express.js - Express for Everyone
Koa.js - Next Generation Node.js Framework
Meteor.js - One Application, One Language
Socket.io - Chat Apps Made Easy with Socket.io
Nest.js - A Nestling of Code

Discover and read more posts from Sonia M
get started
post comments1Reply
pragya pradhan
3 years ago

why do we prefer npm start instead of node index.js