Language: EN

que-es-y-como-usar-npm

Managing Packages with NPM

NPM “Node Package Manager” is one of the tools that you will surely 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 in Node.js projects.

Given that Node.js has become one of the most important tools for software development, NPM is a utility that you will use very frequently. Therefore, it is advisable to learn how to use it.

Main Features of NPM

Package Repository

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

Installation and Management of Packages

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

Scripts

NPM allows you to define custom scripts. These scripts enable 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 so we can use it in Node.js projects.

Creating a Node.js Project

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

npm init

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

Installing Dependencies

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

npm install

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

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

Removing Packages

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

npm uninstall package-name

Running Scripts

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

npm run script-name