Open In App

What is NPM & How to use it ?

Last Updated : 04 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integrated into Node.js projects to extend their functionality.

How to Use NPM?

1. Installation:

If you’ve installed Node.js on your system, NPM is automatically installed along with it. You can verify its installation by opening a terminal or command prompt and typing:

npm -v

This command will display the version of NPM installed on your system.

2. Managing Packages:

  • Installing Packages: To install a package using NPM, navigate to your project directory in the terminal and run the following command:
npm install <package-name>

Replace <package-name> with the name of the package you want to install. NPM will download the package and its dependencies and add them to your project’s node_modules directory.

  • Installing Packages Globally: Some packages are designed to be used globally across multiple projects. To install a package globally, use the -g flag:
npm install -g <package-name>
  • Listing Installed Packages: To see a list of packages installed in your project, run:
npm list

This command will display a tree-like structure of installed packages and their dependencies.

  • Removing Packages: To remove a package from your project, use the uninstall command:
npm uninstall <package-name>

3. Managing Dependencies:

  • Dependency Management: When you install a package using NPM, it automatically manages dependencies by installing the required packages as well. These dependencies are listed in your project’s package.json file.
  • Updating Dependencies: To update dependencies to their latest versions, you can use the update command:
npm update

This command will update all packages in your project to their latest compatible versions.

4. Creating and Publishing Packages:

NPM also allows developers to create their own packages and publish them for others to use. To create a new package, navigate to your project directory and run:

npm init

Follow the prompts to provide information about your package, such as its name, version, and description. Once your package is ready, you can publish it to the NPM registry using the publish command:

npm publish

Conclusion:

NPM is an essential tool for Node.js developers, providing a convenient way to manage packages and dependencies in their projects. Whether you’re installing packages to extend your project’s functionality or creating and publishing your own packages, NPM simplifies the process and streamlines development workflows. By mastering NPM, you can leverage a vast ecosystem of open-source packages and accelerate your development process.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads