Node.js now comes with both npm and npx. You probably have heard about npm. It is a Node Package Manager that you can use to install different open-source packages in your project.
npx on the other hand is like a Node package execution command. It executes a command either from the local node_modules/bin folder or from a central cache. Let’s take a look at it.
If you have the npm version greater than 5.2 then you will also have the npx installed. Just type in npx in your terminal and you will see something like this.
Now let’s try running a Node package with npx. For this, I am going to run a CLI called figlet CLI. I am going to run the following command in my terminal.
npx figlet-cli ‘NodeCLI.com’
Now as soon as I run this command, I will see NodeCLI printed inside my terminal.
The under the hood working is when I run npx figlet-cli command, npx will check if the CLI is installed locally or globally. When it will not find anything, it will go ahead and install the figlet CLI and run it.
npx has made running Node packages a lot easier. You can always run the latest version of any package. Now you don’t have to install your package globally and update it whenever there is an update available.