Skip to content

Instantly share code, notes, and snippets.

@khriztianmoreno
Last active November 19, 2019 00:29
Show Gist options
  • Save khriztianmoreno/15b9c3a52cf16a2b8bd3785b26e26696 to your computer and use it in GitHub Desktop.
Save khriztianmoreno/15b9c3a52cf16a2b8bd3785b26e26696 to your computer and use it in GitHub Desktop.
npx Package Runner

NPX

You can’t be everywhere at once. Neither can node packages. But with a utility called npx — included in npm v5.2 and later — you can interact and run with node modules even if they’re not downloaded locally. The same is true for gists and remote branches of GitHub. As long as npm installed, your work can be wherever you are.

Overview

In this course we are going to be talking about a little utility called npx. If you have npm version 5.2 or above, then you already have npx installed on your machine. It enables you to play around with node packages in a way that wasn’t really easy before.

Have you ever wanted to run a locally installed node_module, but you didn’t really want to jump through hoops to do so? Or how about playing around with a package from the npm registry, but you don’t really want to install it globally yet? Well, npx is the tool for those things. You can even play around with experimental GitHub branches with npx, and so much more.

I find myself using npx all the time. It’s one of those tools, where at first you think, "Ohh that’s simple", but then the more you know about it, it can do so much more than you thought at first. So, sit back and enjoy learning about npx.

Use npx to run locally installed node modules

  1. mkdir npx-test && $_
  2. npm init -y
  3. npm i eslint -D
  4. npm ls eslint
  5. echo "let x = 1; y = 2;" > index.js

Since we previously confirmed that ESLint is installed locally, you might be tempted to start interacting with it on the terminal.

  1. eslint --init

No, that does not work. It doesn't know about my locally installed version. It's looking for something that's globally installed. You could get around this by manually poking into your local Node modules bin folder in order to run ESLint.

  1. ./node_modules/.bin/eslint --init
  2. $(npm bin)/eslint --init
  3. npx eslint --init

Use npx to Temporarily Install and Invoke a Package from npm

Wwe’ll temporarily install and invoke a node package from npm that we don’t already have installed globally or locally. This is useful for commands that you don’t use regularly and you’d like to stay up-to-date or for tools you want to play around with temporarily.

  1. create-react-app
  2. npm ls create-react-app --global
  3. npm ls create-react-app
  4. npx create-react-app playground

Also, it could be handy if you just want to play around with some new tools or ideas before committing to them. For example, let's take a look at devpun. We don't have it installed, but we could start using it with npx devpun.

  1. npx devpun -t react
  2. npx devpun -t react | npx cowsay -f vader However, let's continue to play around. Let's pipe the input of devpun to cowsay, with the Vader Cow file.
  3. npx devpun -t react | npx cowsay -f vader | npx lolcatjs Now, things are getting interesting. Let's continue to play around a bit, and this time, pipe the output to lolcat.js. Wow, colorful.

Using NPX, we're able to play around and experiment with various Node modules without even installing them globally. Pretty cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment