que-es-y-como-usar-npm

Managing Packages with NPM

  • 2 min

NPM “Node Package Manager” is one of the tools that you will probably use most frequently in the development environment.

NPM is a package manager that is primarily used in the Node.js and JavaScript development environment. NPM was created to facilitate the installation, management, and updating of dependencies for Node.js projects.

Since Node.js has become one of the most important tools for software development, likewise NPM is a utility you will use very often. Therefore, it’s good to learn how to use it.

Main Features of NPM

Package Repository

NPM provides a public online repository that hosts a huge number of open-source software packages. Currently, the NPM Registry contains millions of packages available for download and use.

Package Management

NPM makes it easy to install and manage Node.js packages in our projects. To install a package, we simply run a command in the command line and NPM will handle downloading the package and all its dependencies automatically. Furthermore, we can also update or remove packages easily.

Scripts

NPM allows you to define custom scripts. These scripts allow us to automate common tasks, such as running tests, compiling code, or generating documentation.

Basic Usage of NPM

Let’s look at some basic NPM commands to be able to use it in Node.js projects.

Creating a Node.js Project

To create a new Node.js project. We run the following command.

npm init

This creates a package.json file, which is the starting point for creating a Node.js project.

Installing Dependencies

To install all the packages listed as dependencies in the package.json file, we simply do:

npm install
Copied!

The libraries are downloaded and saved in the node_modules folder.

Adding a Package

To install a package, use the following command:

npm install package-name
Copied!

This command will download the package and all its dependencies into your project directory.

Updating Packages

If you want to update a package to the latest available version, use the following command:

npm update package-name
Copied!

Removing Packages

If you no longer need a package in your project, you can remove it using the following command:

npm uninstall package-name

Copied!

Running Scripts

If you have scripts defined in your package.json file, you can run them using the command:

npm run script-name
Copied!