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 gist? 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 firstname.lastname@example.org, 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
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.
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.
You can use gist.github.com 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.
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
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
~/.config/fish/config.fish as needed
<shell> : can be zsh, bash (≥4) (or) fish
$ email@example.com --version
firstname.lastname@example.org is not found. Trying with npx ...
npx: installed 24 in 2.243s
Now, You can try to install any package like
$ npm run email@example.com 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 egghead.io (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
Create your free account to unlock your custom reading experience.