NPX – A Smart Package Runner !

F:\nodejs\examples>npx cowsay -f tux "Hello......"
npx: installed 10 in 6.567s
< Hello...... >
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\

In the above example we have run a command from the latest cowsay package in npm registry. We did not have to install this locally or globally to use this package.

npx is a useful command that can execute NodeJS library commands either from a local node_modules/.bin, or directly from the npm registry. It is part of the npm package since 5.2.0.


Smartly Connects to a World of Development Tools


There are many executable NodeJS libraries which are quite useful as development tools. NPX smartly connects us to a world of NodeJS utility tools without forcing us to install them for our usage.

The NPX enables us to use the latest versions of tools directly from the NPM registry. If it does not find the executable command in our local installations, it temporarily installs the required packages from the npm registry to run the command.

Below are some of the sample use cases where the npx enables us to use a wide range of tools productively from the npm registry.

Tools with One-off usage

Scaffolding libraries to create a base folder structure for instance. Such tools with one-off usage, executing it from the npm registry makes more sense rather than looking for updates each time we look for using it.

//A scaffolding library to create a base react app project 
npx create-react-app  my-react-app 

//To create a base express js project
npx express-generator my-express-app

//To create a base folder structure for a Vue App
npx create-vue-app 

NPX keeps the choice open

There are many utility tools with multiple choice. NPX makes it easy to switch between different libraries.

Multiple options to minify, compress, mangle your js or css libraries for instance.

//Using treaser library
npx terser file.js > file.min.js

//Using uglifyjs library
npx uglifyjs file.js --compress --mangle --output file.min.js

NPX makes the utility tools readily available

The npx makes 100s of utility tools readily available without the need to install different versions and cluttering the local libraries.

A tool to obfuscate your js libraries.

npx javascript-obfuscator main.js -o main.obf.js

A tool which can check the availability of updated versions for your project libraries

npx npm-check-updates

Below are the examples of utility tools very useful for testing and sharing purpose.

http-server : The library can instantly make our static file from a directory available over http server.

local-tunnel : We can make our local port accessible to others. An useful tool for temporary sharing purpose.

npx http-server --port 8080

//temporarily make a local port public 
npx localtunnel --port 8080


Allows us to Run Code using a different Node Version

We can use npx to run a script with a different node versions by specifying the node version as below.

npx -p node@10 npm run build

Its a very handy feature in deed for testing purpose.


Run Code Snippets from Github GIST

With npx we can also execute the node scripts directly from GIST. For example, if we have the following to files in our GIST:

#!/usr/bin/env node

console.log("Hello from GIST");
{"name": "hello-from-gist", "version": "0.0.0", "bin": "./index.js"}

We can execute the script using npx as below:

npx: installed 1 in 12.351s
Hello from GIST


Note : This is an useful feature for sharing a script using GIST. But, we need to be careful while executing such scripts from unknown sources !