paint-brush
npx: npm package runnerby@vali.shah7
19,337 reads
19,337 reads

npx: npm package runner

by Vali ShahDecember 29th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

Company Mentioned

Mention Thumbnail
featured image - npx: npm package runner
Vali Shah HackerNoon profile picture

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.

Background

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 [email protected], 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 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.

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/config.fish as needed

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




$ [email protected] [email protected] 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 [email protected] 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)

Conclusion

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** = **co**ffee + **de**veloper