npx: npm package runner

An introduction to npx, the npm package runner

npm — package runner

What if you could interact with and run node modules without having them downloaded locally? What if you could directly run the code of your GitHub gits? Doesn’t sound possible? It actually is.

In this article, we’ll talk about npx (package runner) — what it is, why we need it, and the use cases where the tool is helpful when running node packages from the command line.


npm (node package manager) has improved a lot over the years and provides a way for developers to have a global and local installation of packages. There are times you are required to look over a package and try out commands, or you might even want to get rid of devDependencies. From npm@5.2.0, npm started installing a new binary alongside the usual npm called npx.

npx : An npm package runner — helps to execute packages without installing explicitly.

$ which npx

If for some reason it’s not available, install or update it as the following:

$ npm install -g npx

npx makes it easy to install and manage dependencies hosted in npm registry. It simplifies the process and provides a better for executables.

$ npx <command> //command: package to be installed

Use cases:

  • To execute one-off commands
  • To use gist based scripts
  • To use different versions of an npm module
  • If you don’t have permission to install it globally.

Execute one-off commands:Uses create-react-app execution to create a new app without installing in global

There are times you wanted to try some CLI tools but it’s annoying to have it installed globally (or) locally just to run it once. npx is a great way for solving this. Using npx <command> to initiate the execution of a package. If <command> is not already in your $PATH, npx will install the package from npm registry and invoke it. npx will not maintain the packages in the globals, So you don't have to worry about polluting your globals.

Tools like create-react-app only called once in a while. If you have these installed in your globals, You might have those outdated by the time when you use. You might end up having to run install every time you wanted to use. npx is a good way to solve this problem.

Execute gist based scripts:gist execution

You can use to share any utility scripts instead of creating a git repository. npx can consider executing any specifier that npm does. You can invoke a gist directly using npx.

Make sure to read through the gists when executing as they could cause serious problems if the script is having some malicious process.

SHELL auto fallback:

You can configure npx to run as your default fallback command when you type something in the command line with an @ and the command is not found. This includes installing packages that were not found in the local prefix either.

Mac OS X by default runs with bash version <4. As OS X did not update to latest version of bash which is licensed under GPLV3. Here is the reason why

Zsh is an alternative for bash which can be used with OS X. Here are the differences for both. bash vs Zsh & Installing and making zsh as your default shell

Restart the shell after the installation. As it’s the first time after the shell update., zsh will ask for a few initial settings. Now, You can set up auto fallback.

To allow the current shell session to have the fallback

$ source <(npx --shell-auto-fallback <shell>)

To install permanently, add npx --shell-aut-fallback <shell>` to your ~/.bashrc, ~/.zshrc, ~/.config/fish/ as needed

<shell> : can be zsh, bash (≥4) (or) fish

$ mocha@4.1.0 --versionmocha@4.1.0 is not found. Trying with npx ...npx: installed 24 in 2.243s4.1.0

Now, You can try to install any package like $ npm run mocha@3.2 which will try to find the webpack in project level if not it will automatically fallback to use npx.

There is a course for npx in (if you are interested)


npx helps to solve the problems of installing unnecessary packages just to look through. It’s an easy way to avoid versioning problems and dependency issues. Like I always say, Will be back with more interesting topics. Cheers!

code = coffee + developer

npx: npm package runner was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

Publication date: 
01/31/2019 - 14:47

The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.