AI Workshop: learn to build apps with AI →
npm: The npx Node Package Runner

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


npx is a command that ships with npm (since 5.2, July 2017). It runs packages without installing them globally.

You can also install npx as a standalone package without npm.

npx lets you run code built with Node and published through the npm registry.

Easily run local commands

Node developers used to publish most of the executable commands as global packages, in order for them to be in the path and executable immediately.

That made it hard to use different versions of the same tool in different projects.

Running npx commandname automatically finds the correct reference of the command inside the node_modules folder of a project, without needing to know the exact path, and without requiring the package to be installed globally and in the user’s path.

Installation-less command execution

There is another great feature of npm, which allows running commands without first installing them.

This is pretty useful, mostly because:

  1. you don’t need to install anything
  2. you can run different versions of the same command, using the syntax @version

A typical demonstration of using npx is through the cowsay command. cowsay will print a cow saying what you wrote in the command. For example:

cowsay "Hello" will print

 _______
< Hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

If you do not have cowsay installed, running it directly will fail. With npx you can run it without installing it first:

npx cowsay "Hello"

Other common uses:

  • running the vue CLI tool to create new applications and run them: npx vue create my-vue-app
  • creating a new React app using create-react-app: npx create-react-app my-react-app

and many more.

Once downloaded, the downloaded code is wiped.

Run some code using a different Node version

Use the @ to specify the version, and combine that with the node npm package:

npx node@6 -v #v6.14.3
npx node@8 -v #v8.11.3

This helps to avoid tools like nvm or the other Node version management tools.

Run arbitrary code snippets directly from a URL

npx does not limit you to the packages published on the npm registry.

You can run code that sits in a GitHub gist, for example:

npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32

Of course, you need to be careful when running code that you do not control, as with great power comes great responsibility.

Lessons in this unit:

0: Introduction
1: How to use or execute a package installed using npm
2: npm dependencies and devDependencies
3: How to fix the "Missing write access" error when using npm
4: npm can install packages in the parent folder
5: Install an older version of an npm package
6: Find the installed version of an npm package
7: How to test an npm package locally
8: npm global or local packages
9: What are peer dependencies in a Node module?
10: `npm run dev` is a long-running program
11: Semantic Versioning using npm
12: Uninstalling npm packages with `npm uninstall`
13: An introduction to the npm package manager
14: ▶︎ The npx Node Package Runner
15: The package.json guide
16: The package-lock.json file
17: What is pnpm?
18: Should you commit the node_modules folder to Git?
19: Update all the Node dependencies to their latest version
20: Where does npm install the packages?
21: Bumping Node.js dependencies
22: Run package.json scripts upon any file changes in a folder