You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
14 Hrs
53 Min
21 Seg
Curso de Fundamentos de Node.js

Curso de Fundamentos de Node.js

Oscar Barajas Tavares

Oscar Barajas Tavares

Gesti贸n de Paquetes con NPM

5/20
Resources

Package management with NPM is a fundamental skill for any modern JavaScript developer. This system not only makes it easy to incorporate third-party code into our projects, but also allows us to share our own creations with the global community of developers, thus fostering a collaborative and efficient ecosystem.

What is NPM and why is it so important?

NPM (Node Package Manager) is the tool that allows us to install, uninstall and manage the packages that the community builds in order to incorporate them into our projects. It works as a huge library of resources that facilitates the development, avoiding us to "reinvent the wheel" constantly.

The npmjs.com website is the central point where we can find this vast collection of resources. From simple validators to complete frameworks, practically any functionality we need probably already exists as an NPM package.

Most of these packages are open source, which means we can:

  • Access the source code.
  • Understand how it works internally.
  • Implement it according to our needs.
  • Learn by analyzing other developers' code.

How to start using NPM?

NPM is installed by default when we install Node.js on our system. We can verify its presence and version with a simple command:

npm -v

To initialize a project with NPM, we must run:

npm init

This command starts an interactive process that will ask us for information to configure our project:

  • Project name
  • Version
  • Description
  • Entry point
  • Test commands
  • Repository
  • Keyword
  • Author
  • License

All this information will be stored in a file called package.json, which will function as the central configuration of our project.

How to install and use packages?

Once our project is initialized, we can start installing packages. For example, if we want to install a package to validate if a value is numeric:

npm install is-number# or for shortnpm i is-number.

This command performs several important actions:

  1. Downloads the package and saves it to the node_modules folder.
  2. Updates the package.json file by adding the dependency
  3. Creates or updates the package-lock.json file that keeps an accurate record of installed versions

Implementing the package in our code

Once installed, we can use the package in our code:

const isNumber = require('is-number');
console.log(isNumber(5)); // trueconsole.log(isNumber('hello')); // false

Best practices when working with NPM

Ignoring node_modules in version control

The node_modules folder can become extremely large, as it contains all installed packages and their dependencies. For this reason, it is standard practice to exclude it from our Git repository.

To do this, we create a .gitignore file in the root of our project. We can use tools like gitignore.io to generate a file suitable for Node.js projects.

Create scripts to facilitate common tasks

NPM allows us to define scripts in our package.json to execute frequent tasks:

"scripts": { " start": "node main.js"}

These scripts can be run with:

npm run start

Scripts are a powerful way to standardize commands that are frequently used in the development, testing or deployment of our applications.

NPM is a fundamental tool in the JavaScript ecosystem that greatly facilitates the development of modern applications. Mastering its use will allow you to take advantage of the work of thousands of developers and contribute your own creations to this vibrant community. Have you created a package for NPM or do you have any questions about how to implement one in particular? Share your experience in the comments!

Contributions 0

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?