LEARNNode CLI Automation

npx — Node Package Execution Command

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.

npx

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.

node package executioner

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.

figlet CLI

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.

Wrapping Up

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.

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)