Published on

Aliasing Npx Commands


In one of the previous releases of npm the npx (Node Package eXecutor)[this seems to be what the acronym stands for] was introduced. This allows people to run an npm package that is more of a command line utility without having to install it. For example normally if you wanted to use create-react-app to generate a React app you would need to:

npm install -g create-react-app
create-react-app my-new-app

Using Npx we can now instead write the following:

npx create-react-app my-new-app

This is great as it works now on systems that are locked down where installing of new packages are restricted (if you do not setup your npm to allow none-sudo installs of global packages as described in a previous post).

This can become a pain if you want to keep running the same command as you have to type a little more. You can and probably should just npm install -g requiredCliTool then. But there is another option - create a command line alias for it.

Using the create-react-app example this would be done as follows in your .bashrc/.zshrc/.profile file as follows:

alias create-react-app='npx create-react-app'

You can now run create-react-app as if you npm install -gd it. You might say but hey will it work offline?! The answer is it likely will as per the documentation:

Executes <command> either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for <command> to run.

By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that. If <command> is not found, it will be installed prior to execution.