Thursday, May 2, 2024

NodeJS NPM Package Manager

In this article we'll see how to use npm in NodeJS to install packages which can then be used in the application.

NPM (https://www.npmjs.com/) is the standard package manager for Node.js. Using npm you can download and install dependencies of your project. It also acts as an online registry, with more than two million packages, npm Registry is the largest software registry in the world.

Check this post- How to Setup a Node.js Project to see how to setup a NodeJS project with a package.json file.

How to install NPM

When you install NodeJS, NPM package manager is also installed as part of Node.js installation. To check if it is successfully installed, run the following command from command prompt.

npm -v

It should display version number for the NPM installation.

Installing packages using npm

To install any specific package you can use the following command

npm install <package-name>

You can control where and how the specified packages get saved by using some additional flags with npm install

-P, --save-prod: Package entry will appear in the dependencies section of the package.json file. You'll use this option if you want the installed package to be part of production build too. This is also the default option.

-D, --save-dev: Package entry will appear in the devDependencies section of the package.json file. You'll use this option if you want the installed package to be used during development only.

--no-save: With this option package will be installed but does not add the entry to the package.json file dependencies.

--save-optional: Installs and adds the entry to the package.json file optionalDependencies. If package's entry is in the optionalDependencies section that means dependency can be used if found, but installation doesn't fail if it cannot be found or fails to install. But it is your program's responsibility to handle the lack of the dependency.

--no-optional: Will prevent optional dependencies from being installed

For example, if you want to install ExpressJS

npm install express

Then you need to import 'express' package in your JS file

const express = require('express');

Note that when you use npm install command, packages are installed locally meaning installed to be used with in the specific application. Installed packages are kept in a node_modules directory which is present with in the node application for which packages are installed.

If you want to install packages globally use -g option with the install command. For example, if you want to install express module globally.

npm install express -g

Installing all dependencies

NPM can install all the dependencies of a project through the package.json file. If package.json file is already present with dependencies, devDependencies sections then npm install command will install all the packages required in the project.

Uninstalling packages

To uninstall a package from your project use the following command

npm uninstall <package name>

The package entry will also be removed from the list of dependencies in the package.json file.

Updating package

If you want to update a single package use the following command

npm update <package-name>

If you use the following command

npm update

npm will check all packages for a newer version that satisfies your versioning constraints.

That's all for this topic NodeJS NPM Package Manager. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Creating HTTP server in Node.js
  2. NodeJS Event Loop
  3. JavaScript Arrow Function With Examples
  4. JavaScript Array and Object Destructuring

You may also like-

  1. How to Setup Angular
  2. Creating New Component in Angular
  3. Creating a New React Project - create-react-app
  4. Java ThreadLocal Class With Examples
  5. Java String Interview Questions And Answers
  6. java.lang.UnsupportedClassVersionError - Resolving UnsupportedClassVersionError in Java
  7. Dependency Injection in Spring Framework
  8. Spring Boot Microservices Introduction

No comments:

Post a Comment