Differences between npm and npx

npm (node package manager) is the default dependency/package manager that comes with the installation of Node.js. It offers developers a way to install packages globally and locally.

Occasionally, you may want to explore a specific package and experiment with some commands. However, doing so without installing the dependencies in your local node_modules folder is not possible.

This is where npx comes into play.

In this article, we will explore the distinctions between npm and npx and discover how to maximize their potential.

First, let’s gain an understanding of npm and its capabilities.

npm: the package manager, npm serves two main purposes

Firstly, it functions as an online repository for publishing open-source Node.js projects.

Secondly, it is a command-line interface (CLI) tool that facilitates the installation of these packages, as well as the management of their versions and dependencies. npm boasts hundreds of thousands of Node.js libraries and applications, with new additions being made regularly.

npm itself does not execute any packages. To run a package using npm, you need to specify the package in your package.json file.

When executables are installed via npm packages, npm creates links to them:

  • Local installs generate links in the ./node_modules/.bin/ directory.
  • Global installs generate links in the global bin/ directory (e.g., /usr/local/bin on Linux or %AppData%/npm on Windows).

To execute a package with npm, you can either provide the local path:

$ ./node_modules/.bin/your-package

Or add the locally installed package to your package.json file in the scripts section:

{
   "name": "test-application",
   "version": "1.0.0",
   "scripts": {
     "test-package": "test-package"
   }
}

Then you can run the script using npm run:

npm run test-package

As you can see, running a package with npm requires some ceremony.

Fortunately, npx comes to the rescue.

npx: the package runner
Since npm version 5.2.0, npx is bundled with npm, making it a standard tool nowadays.

npx is also a CLI tool designed to simplify the installation and management of dependencies hosted in the npm registry.

With npx, it’s effortless to run any Node.js-based executable that you would typically install via npm.

You can check if npx is already installed for your current npm version by running the following command:

$ which npx

If it’s not installed, you can install it like this:

$ npm install -g npx

Once you’ve confirmed its installation, let’s explore a few use cases that demonstrate the usefulness of npx.

  1. Running a locally installed package easily
    To execute a locally installed package, simply type:

$ npx test-package

npx will check if or exists in $PATH or in the local project binaries. If found, it will execute it.

Executing packages without prior installation
Another significant advantage is the ability to execute a package that hasn’t been previously installed.

Final conclusion

npx proves to be a valuable tool in tackling versioning, dependency conflicts, and the installation of unnecessary packages that are only required for experimentation purposes.

Furthermore, it offers a straightforward and convenient approach to executing packages, commands, modules, as well as GitHub gists and repositories.

If you haven’t had the chance to explore the capabilities of npx yet, now is the perfect time to dive in and experience its benefits firsthand!

Leave a Reply

Your email address will not be published. Required fields are marked *