I’ve just finished watching Elijah Manor’s Egghead course about npx. It really useful, not just for the info about npx but also for all the npm tricks I picked up too. Plus it was less than 20 minutes in length! In an attempt to keep this info in my brain for as long as possible, here are my notes from the course. The course is currently free and available to watch on Egghead.

What is npx?

npx was introduced in npm version 5.2. It’s an additional command-line tool that comes with npm, that enables you to execute packages and code without globally installing them, as well as lots of other helpful tasks.

For me, the most useful things are:

  • being able to temporarily install and run an npm package without globally installing it
  • easily running commands from packages locally installed in the node_modules folder

Running commands from node_modules

Say you have ESLint installed in your local project, but not globally on your machine. If it were globally installed, you could run eslint --init to initialise ESLint in your project.

But what if you don’t want to globally install it? Here are three methods to run it from your node_modules folder:

// run the binary with the full node_modules path
./node_modules/.bin/eslint --init

// use npm environment variables and bash brace expansion
$(npm bin)/eslint --init

// npx
npx eslint --init

Which one is the simplest? The npx one!

Temporarily installing a package

Temporarily install and invoke a package from npm using npx. You might want to do this if you:

  • use a package too infrequently to justify installing (e.g. generators)
  • always want the latest version when you run it: using npx ensures you will have the latest version without remembering to update the global version
  • want to avoid global installs

create-react-app is a good candidate for this. All you need to do is invoke the command with npx, e.g.:

`npx cowsay moo`

Try out different versions of a package

npx is handy if you want to run a different version of a package.

You can list all the versions of a package with npm v <package-name> versions, or just get the latest with npm v <package-name> version.

When you know what version you want, just @ it on the end:

npx cowsay@2.0.0 moo

Using npm environment variables

npm run env in a project with a package.json file will display lots of useful environment variables that belong to npm. They are accessible to scripts within the package.json “scripts” object. To give npx access to them, use the -c flag.

Other cool things you can do

You can check the docs for syntax.

  • Run a package or command in different node versions than the one installed on your system. This can avoid the need for node package managers.
  • Execute branches of repos rather than just master.
  • Execute gists npx gist-url. Your gist will need a package.json file in it. Don’t forget to check 3rd party gists for malicious code before executing!

npm Tips

  • Append -s to the end of an npm command to prevent all the noisy error logging (s = silent)
  • npm ls --global - list all globally installed npm modules on your machine
  • npm ls -g package-name - check if a particular package is globally installed
  • npm ls package-name - check local node_modules for a package
  • npm v package-name versions - lists all available versions of a package
  • npm v package-name version - lists the latest version number for a package
  • npm i babel{-cli,-preset-env,-other} - use bash brace expansion to install multiple packages with similar names, i.e. in this example babel-cli, babel-preset-env and babel-other
  • npm run env - in a project with a package.json file, displays lots of useful environment variables that npm uses
  • npm repo - will open the repository page for the package you’re looking at if it’s set in the package.json