NPM: The Node Package Manager

Aditi Dosi
3 min readSep 30, 2022

--

npm is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

NPM is the default dependency management tool for the NodeJS projects. It uses ‘package.json’ for configuring project details. The configurations include dependencies, scripts tasks, repository location and many other stuffs.

As a dependency management tool, it not only help us download and include direct dependencies but, also takes care of the dependencies of these direct dependencies internally.

Installing All External Dependencies using NPM

We use three major types of dependencies in NodeJS projects:
1. dependencies — Includes libraries that needs to be bundled with the app in production.
2. devDependencies — Includes build-libraries, testing-libraries required for development and quality checks. We do not need these in our production builds.
3. optionalDependencies — Its a less commonly used field but, as the name suggest it includes optional dependencies. In other words, it says the application has been built to work even if these dependencies are missing.

We can find these categories in ‘package.json’ file. And, to install or download all these listed dependencies for a project we can use the following command :

npm install

Adding Individual Dependencies

1. Local installation : Adding a library for a specific project

npm install <package-name>

//To add ‘express’ library into our project

npm install express

Using the above command, we can add an external package under ‘dependecies’ list in ‘package.json’. This downloads the package into node-modules subfolder of the current project.

The ‘install’ command comes with following key options for specifying type of the dependency :

  1. –save-dev : It adds the package under list of ‘devDependencies’ in package.json.
  2. –no-save : It does not add the downloaded package as a project dependency in package.json.
  3. –save-prod or –save : It’s the default option and we need not specify it explicitly. And, it adds the installed package under ‘dependencies’ list.

2. Global Installation — Adding a library to share across projects

npm install -g <package-name>

Using ‘-g’ option as shown above, we can install external libraries in global locations. This is useful for libraries shared across multiple projects. Generic development tools like ‘create-react-app’, ‘HTTP-server’ etc are good candidates for global installations.

The -g option installs the library into the global location. The global location can be found using :

F:\nodejs\node01\first-project>npm root -g

C:\Users\Akshyat\AppData\Roaming\npm\node_modules

Removing Individual Dependencies

The command to remove a package from the node-modules directory.

//For removing from local node-modules directory

npm uninstall <package-name>

//For removing from global node-modules directory

npm uninstall -g <package-name>

The command to remove a package from local node-modules and dependencies list in package.json :

//Command to remove from dependencies list

npm uninstall -save <package-name>

//Command to remove from devDependencies list

npm uninstall -save-dev <package-name>

Summary

Apart from dependency management, npm along with package.json help us manage various aspects of project development. It helps us in managing various command line scripts, publish the package, and maintaining many other project-related details. We will discuss more on this as part of the article on package.json

--

--