It's #FrontendFriday

It's #FrontendFriday - What's the difference between NPM and NPX?

Most front-end developers should be familiar with NPM. But there is another tool besides NPM that Node provides us with: NPX. In this #FrontendFriday In this article I would like to introduce you to NPX and show you the differences to NPM.

What is NPM?

NPM stands for Node Package Manager. It is the standard tool for managing packages and dependencies in Node.js. NPM can be used to install and update new packages and manage dependencies. It is normally used to install packages globally or locally in a project. This is done using the "npm install" command.

What is NPX?

Since NPM version 5.2, the NPX tool is automatically supplied. NPX stands for "NPM package executor". NPX is mainly used to execute commands contained in a package without actually installing the package. It also allows a package to be executed only once and then deleted. This saves time as it eliminates the need to install a package globally or locally. This means that the "npx" command only installs a package once.

Differences between NPM and NPX

Let's compare the use of both tools using an example. Our goal is to run a test with Jest in an existing project.

Example NPM

To run the tests, we must first install the Jest test framework on our system using "npm install jest". As soon as the tool is installed on the system, you can run the tests with "npm test".

Example NPX

To run Jest with NPX, all we have to do is enter "npx jest". This will download Jest once and run the tests. This saves the time and disk space needed to install Jest globally or locally. Another major advantage of NPX is that you can run packages with different versions without having to install them globally or locally. This means that you can run an older version of Jest with "npx jest@28.1.3" to check whether a breaking change has occurred during the update to Jest 29. This procedure can of course also be used to test new package versions (e.g. latest) before finally integrating them into the project.

To summarize, NPM is mainly used for managing packages and dependencies in Node.js development, while NPX is used for the one-time execution of commands from a package. Both tools are indispensable for Node.js development and allow developers to do their work faster and more efficiently.

 

Learn more about software development

Alexander Grünwald

About ME

All contributions from Alexander Grünwald

Learn more

Further information on our website and in our newsletter

Arrow up