Language: EN

que-es-y-como-usar-npx-en-npm

What is and how to use NPX

NPX is a command executor for Node.js that was introduced in NPM version 5.2.0. Its name is an acronym for “Node Package eXecutor” and its main goal is to facilitate the execution of package commands without having to install them globally.

In summary, the NPX tool automatically resolves the location of the package and runs it. In this way, it is not necessary to have it installed globally.

This way we avoid having global packages installed, which we already know can generate conflicts between projects, or cause problems when sharing projects between teams and/or people.

NPX has been an important improvement in the Node.js and NPM environment. It allows us to execute package commands without the need to install them globally, which simplifies dependency management and improves project portability.

Advantages of using NPX in an NPM course

NPX offers several advantages in the context of an NPM course. Some of them are:

  • Avoids global package installation: With NPX it is no longer necessary to install packages globally. Your computer stays cleaner 😉.

  • Facilitates the management of package versions: NPX allows you to run commands with specific versions of packages, which is useful for teaching concepts related to compatibility and version management.

  • Avoids dependency conflicts: By not installing packages globally, NPX avoids dependency conflicts between different projects or exercises of a course. Each execution of NPX is performed in an isolated environment and does not affect other package installations.

Installation

NPX comes pre-installed with NPM since version 5.2.0, so it is not necessary to install it separately.

You can check that you have it properly installed by running this command.

npx -v

Basic usage of NPX

The simplest way to use NPX is to run a command followed by the name of the package you want to execute.

For example, let’s imagine that we want to run the create-react-app command, which creates a new React application. The package author makes this tool available to us.

Previously, we would have had to install the package globally,

npm install -g create-react-app

But with NPX, we can do the same as follows,

npx create-react-app my-application

In this case, NPX will look for the create-react-app package in local directories. If it does not find it, it will download it temporarily, run it, and remove it after use.

This means that no files will be permanently installed on your system, which is a very important advantage over the previous method of global installation, which forced you to go installing things and more things throughout your team.

Running specific commands from a package

NPX also allows you to run specific commands from a package without installing the package itself.

You can use the syntax npm <package> <command> to run a specific command from a particular package.

For example, if you have a package called my-package that provides a command my-command, you can run it directly using NPX as follows:

NPX my-package my-command

Running commands with specific versions

In addition to running package commands without globally installing them, NPX also allows us to specify the version of a package we want to use.

This can be useful when we need to run commands with specific versions to ensure compatibility or test new features.

To run a command with a specific version of a package, you can use the following syntax:

npx <package>@<version> <command>

For example, if you want to run the my-command command from version 1.0.0 of my-package, you can do so as follows:

npx my-package@1.0.0 my-command

NPX will temporarily download version 1.0.0 of my-package, run the my-command command, and remove it after use.